regular expression 5 usage
by webproger on 2020-03-22
<JavaScript>
1. Creating a regular expression
literal format: aRegexp = /pattern/flags;
construction function: aRegexp = new RegExp("pattern"[, "flags"]);

2. Methods that use regular expressions
aRegexp.exec(aString);
aRegexp.test(aString);
aString.match(aRegexp);
aString.replace(aRegexp[, aNewSubStr]);
aString.replace(aRegexp[, aFunction]);
aString.search(aRegexp);
aString.split([aRegexp[, limit]]);
* When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary.:
re = new RegExp("\\w+") <=> re = /\w+/
When using the literal format, the forward slash should be escaped.:
re = new RegExp("a/b" ) <=> re = /a\/b/

<PHP PCRE>
array preg_grep ( string pattern, array input [, int flags] )
int preg_match_all ( string pattern, string subject, array &matches [, int flags [, int offset]] )
mixed preg_match ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )
string preg_quote ( string str [, string delimiter] )
mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
array preg_split ( string pattern, string subject [, int limit [, int flags]] )


procedure

1. string
a'b"c/d

2. regular expression
'\w"\w/

3-1. for JavaScript literal format
Escape /
r = /'\w"\w\//i;

3-2. for JavaScript construction function
Escape \ and "
r = new RegEx("'\\w\"\\w/","i");

3-3. for PHP
Escape \ and ' and /
$r = '/\'\\w"\\w\//i';