Open petrosh opened 9 years ago
str = 'Twas the night v.2! "Xmòas...'
# keep only numbers and letters and SPACE
newstr1 = str.replace /[^0-9a-zA-Z ]/g, ''
# Strip words of 1-3 length
newstr2 = newstr1.replace /\b[0-9a-zA-Z]{1,3}\b/igm, ''
# Space of length 1-3 > to dash
newstr = newstr2.replace /\b[ ]{1,3}\b/igm, '-'
console.log('---')
console.log(str)
console.log(newstr1)
console.log(newstr2)
console.log(newstr)
Compiled
var newstr, newstr1, newstr2, str;
str = 'Twas the night v.2! "Xm\xF2as...';
newstr1 = str.replace(/[^0-9a-zA-Z ]/g, '');
newstr2 = newstr1.replace(/\b[0-9a-zA-Z]{1,3}\b/gim, '');
newstr = newstr2.replace(/\b[ ]{1,3}\b/gim, '-');
console.log('---');
console.log(str);
console.log(newstr1);
console.log(newstr2);
console.log(newstr);
Result
Twas the night v.2! "Xmòas...
Twas the night v2 Xmas
Twas night Xmas
Twas-night-Xmas
var prova='\W/"16f319edf77bc08e0abdd3be3ff9411e"',
urli = 'https://api.github.com/repos/46/fork-n-play/GFL/165/pulls',
etag = RegExp(/W\/"(.*?)"/).exec(prova);
if(etag) localStorage.setItem('fnp.etag.' + urli.replace(/\W+/g, ""), etag[1]);
// fnp.etag.httpsapigithubcomrepos46forknplayGFL165pulls: "16f319edf77bc08e0abdd3be3ff9411e"
With fetch
var lastModified = localStorage.getItem('etag|' + apiRef('master')) || null;
var options = { headers: {'Accept': 'application/vnd.github.v3+json'} };
if (lastModified) options.headers['If-None-Match'] = lastModified;
var apiRef = function (branch) {
return ['https://api.github.com', 'repos', repoOwner, repoName, 'git/refs/heads', branch].join('/');
};
fetch(apiRef).then(function (result) {
var etag = result.headers.get('ETag').replace(/W\//g, '');
localStorage.setItem('naledi|etag|' + branch, etag);
return result.json();
});
exec, match
search and return info
/xy+/.exec( 'yyxyy' );
// {0: "xyy", index: 2, input: "yyxyy", length: 1}
'xyyzxyzz'.match( /xy+/ );
// { 0: "xyy", index: 0, input: "xyyzxyzz", length: 1 }
test
search and return boolean
/xy+/.test( 'yyxyy' );
// true
search
search and return index (or -1
)
'xyyzxyzz'.search( /xy+/ );
// 0
replace
search and replace first match
'xyyzxyzz'.replace( /xy+/, 'U' )
// "Uzxyzz"
split
split a string according to regexp
'xyyzxyzz'.split( /xy+/ )
// ["", "z", "zz"]
g
find all matches
`yxxxyxyxx`.match( /x+/g )
// ["xxx", "x", "xx"]
i
non case sensistive
x = new RegExp( 'x' )
// /x/
xX = new RegExp( 'x', 'i' )
// /x/i
x.test( 'XY' )
// false
xX.test( 'XY' )
// true
Regex search
<div>[^>]*>
select all <div>...</div>
^\n{2,}
select double blank linesHex colors
// find
(#[0-9A-F]{3,6})
// replace
mix($1,$maroon,40%)
id
to timestamp
arrayin Atom from:
id: 1582402979253.295
to:
timestamp:
- 1582402979253
use search:
^ id: ([0-9]{13}).[0-9]{1,5}
and replace:
timestamp:\n - $1
timestamp
array to unidimensionalin Atom from:
timestamp:
- 1582402979253
to:
timestamp: 1582402979253.295
use search:
^ timestamp:\n - ([0-9]{13})
and replace:
timestamp: $1
Regular expressions
Examples
content.replace(/<p><br \/><b><a href=\"(.*?)\">Leggi<\/a> su www.example.it<\/b><\/p>/g,'');
RegExp(/https:\/\/api.github.com\/repos\/(.*?)\/issue/).exec(u)[1];
returnfullname
\b
: a word break and will work for both spaces and end of lines.(^|\s)
: capturing group or start of string or space./\b(stackoverflow)\b/
: example exact string in class list./\*?([a-z])$/
: any characters and last character.'13d'.split(/\*?([a-z])$/)=['13','d','']
Links
Remove Blank Lines
Double blank lines
Sanitize ETag regexr.com
src
attribute from HTML string regexr.comGreedy quantifiers
Reluctant quantifiers
Possessive quantifiers
Commit checker
Link: our project ID is LICGLM, followed by a hyphen, the commit types, a colon and a space, then finally a message. The total commit length is limited to 80 characters.