jQuery Regular Expression

By ukmodak | April 29th 2021 06:09:07 AM | viewed 77 times
Expression Description Example
Modifiers
g Perform a global match (find all matches rather than stopping after the first match) var str = "Is this all there is?"; var patt1 = /is/g; var result = str.match(patt1); alert(result); // result:is,is
i Perform case-insensitive matching var str = "Visit W3Schools"; var patt1 = /w3schools/i; var result = str.match(patt1); alert(result); result:// W3Schools
m Perform multiline matching var str = "\nIs th\nis it?"; var patt1 = /^is/m; var result = str.match(patt1); alert(result) // result://is
Brackets
[abc] Find any character between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find any character between the brackets (any digit)
[^0-9] Find any character NOT between the brackets (any non-digit)
(x|y) Find any of the alternatives specified
Metacharacters
. Find a single character, except newline or line terminator var str = "That's hot!"; var patt1 = /h.t/g; var result = str.match(patt1); alert(result);// result:hat,hot
\w Find a word character var str = "Give 100%!"; var patt1 = /\w/g; var result = str.match(patt1); alert(result); //G,i,v,e,1,0,0
\W Find a non-word character var str = "Give 100%!"; var patt1 = /\W/g; var result = str.match(patt1); alert(result); // ,%,!
\d Find a digit var str = "Give 100%!"; var patt1 = /\d/g; var result = str.match(patt1); alert(result); // 1,0,0
\D Find a non-digit character var str = "Give 100%!"; var patt1 = /\D/g; var result = str.match(patt1); alert(result); // G,i,v,e, ,%,!
\s Find a whitespace character var str = "Is this all there is?"; var patt1 = /\s/g; var result = str.match(patt1); alert(result); // , , ,
\S Find a non-whitespace character var str = "Is this all there is?"; var patt1 = /\S/g; var result = str.match(patt1); //I,s,t,h,i,s,a,l,l,t,h,e,r,e,i,s,?
\b Find a match at the beginning/end of a word var str = "Visit W3Schools"; var patt1 = /\bW3/g; var result = str.match(patt1); //W3
\B Find a match not at the beginning/end of a word var str = "Visit W3Schools"; var patt1 = /\BSchool/g; var result = str.match(patt1); // School
\0 Find a NUL character var str = "Visit W3Schools.\0Learn JavaScript."; var patt1 = /\0/; var result = str.search(patt1); //16
\n Find a new line character var str = "Visit W3Schools.\nLearn JavaScript."; var patt1 = /\n/; var result = str.search(patt1); //16
\f Find a form feed character var str = "Visit W3Schools.\fLearn JavaScript."; var patt1 = /\f/; var result = str.search(patt1); // 16
\r Find a carriage return character
\t Find a tab character
\v Find a vertical tab character
\xxx Find the character specified by an octal number xxx
\xdd Find the character specified by a hexadecimal number dd
\udddd Find the Unicode character specified by a hexadecimal number dddd
Quantifiers
n+ Matches any string that contains at least one n var str = "Hellooo World! Hello W3Schools!"; var patt1 = /o+/g; var result = str.match(patt1); alert(result);// result:ooo,o,o,oo
n* Matches any string that contains zero or more occurrences of n var str = "Hellooo World! Hello W3Schools!"; var patt1 = /lo*/g; var result = str.match(patt1); alert(result);// result:l,looo,l,l,lo,l
n? Matches any string that contains zero or one occurrences of n var str = "1, 100 or 1000?"; var patt1 = /10?/g; var result = str.match(patt1); alert(result);// result:1,10,10
n{X} Matches any string that contains a sequence of X n's
n{X,Y} Matches any string that contains a sequence of X to Y n's var str = "100, 1000 or 10000?"; var patt1 = /\d{3,4}/g; var result = str.match(patt1) alert();// result:100,1000,1000
n{X,} Matches any string that contains a sequence of at least X n's
n$ Matches any string with n at the end of it var str = "Is this his"; var patt1 = /is$/g; var result = str.match(patt1); alert(result);// result:is
^n Matches any string with n at the beginning of it
?=n Matches any string that is followed by a specific string n var str = "Is this all there is"; var patt1 = /is(?= all)/; var result = str.match(patt1); alert(); // result:is
?!n Matches any string that is not followed by a specific string n var str = "Is this all there is"; var patt1 = /is(?! all)/gi; var result = str.match(patt1); alert(result); //result:Is,is
Object Properties
constructor Returns the function that created the RegExp object's prototype var patt = new RegExp("Hello World", "g"); var res = patt.constructor; alert(res) //result: function RegExp() { [native code] }
global Checks whether the "g" modifier is set var str = "Visit W3Schools!"; var patt1 = /W3S/g; var res = patt1.global; alert();// result:true
ignoreCase Checks whether the "i" modifier is set var str = "Visit W3Schools!"; var patt1 = /W3S/i; var res = patt1.ignoreCase; alert(res);// result:true
lastIndex Specifies the index at which to start the next match var str = "The rain in Spain stays mainly in the plain"; var patt1 = /ain/g; var res = patt1.lastIndex; alert(res); // result :8,17,28,43
multiline Checks whether the "m" modifier is set
source Returns the text of the RegExp pattern var str = "Visit W3Schools"; var patt1 = /W3S/g; alert(patt1.source);// result:W3S
Object Methods
compile() Deprecated in version 1.5. Compiles a regular expression
exec() Tests for a match in a string. Returns the first match var str = "The best things in life are free"; var patt = new RegExp("e"); var res = patt.exec(str); // e
test() Tests for a match in a string. Returns true or false var str = "The best things in life are free"; var patt = new RegExp("e"); var res = patt.test(str); alert(res)// result:true
toString() Returns the string value of the regular expression var patt = new RegExp("Hello World", "g"); var res = patt.toString(); alert(res);// result:/Hello World/g

Example

validation Name Example
email var checkEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; var input ="uu@yahoo.com"; var result = input.match(checkEmail); alert(result); // true
mobile var checkMobile = /^[(017)|(018)|(016)|| (019)||(015)]{3}[0-9]{8}$/; var input =01822210098; var result = input.match(checkMobile); alert(result); // true
bONEandALL