petrosh / snippetrosh

Notes with Issues
MIT License
1 stars 3 forks source link

RegExp #15

Open petrosh opened 9 years ago

petrosh commented 9 years ago

Regular expressions

Examples

Links

Remove Blank Lines

^(?:[\t ]*(?:\r?\n|\r))+

Double blank lines

^\n{2,}

Sanitize ETag regexr.com

^[W/"]{1,}|"

src attribute from HTML string regexr.com

[^[\src="]+(?=")

Greedy quantifiers

X?  X, once or not at all
X*  X, zero or more times
X+  X, one or more times
X{n}    X, exactly n times
X{n,}   X, at least n times
X{n,m}  X, at least n but not more than m times

Reluctant quantifiers

X?? X, once or not at all
X*? X, zero or more times
X+? X, one or more times
X{n}?   X, exactly n times
X{n,}?  X, at least n times
X{n,m}? X, at least n but not more than m times

Possessive quantifiers

X?+ X, once or not at all
X*+ X, zero or more times
X++ X, one or more times
X{n}+   X, exactly n times
X{n,}+  X, at least n times
X{n,m}+ X, at least n but not more than m times

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.

(LICGLM)(-)(\d{2,})( )
(chore|feat|docs|fix|refactor|style|test|sonar|hack|release)(:)( )(.{0,80})
petrosh commented 9 years ago

RegExp Sanitize Titles

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
petrosh commented 8 years ago

RegExp ETags

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();
});
petrosh commented 6 years ago

JS RegExp intro

Methods

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"]

Modifiers

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
petrosh commented 6 years ago

Atom

Regex search

Hex colors

// find
(#[0-9A-F]{3,6})
// replace
mix($1,$maroon,40%)
trasparente commented 4 years ago

From id to timestamp array

in Atom from:

  id: 1582402979253.295

to:

  timestamp:
    - 1582402979253

use search:

^  id: ([0-9]{13}).[0-9]{1,5}

and replace:

  timestamp:\n    - $1

From timestamp array to unidimensional

in Atom from:

  timestamp:
    - 1582402979253

to:

  timestamp: 1582402979253.295

use search:

^  timestamp:\n    - ([0-9]{13})

and replace:

  timestamp: $1