The RegExp object allows defining patterns to search for in strings. It has methods like test(), exec(), and compile() to search strings and return matches. test() returns true/false if the pattern is found, exec() returns the matched text or null, and compile() changes the search pattern. The 'g' flag can be used with exec() to find all matches in a string.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
19 views3 pages
29 RegExp Object
The RegExp object allows defining patterns to search for in strings. It has methods like test(), exec(), and compile() to search strings and return matches. test() returns true/false if the pattern is found, exec() returns the matched text or null, and compile() changes the search pattern. The 'g' flag can be used with exec() to find all matches in a string.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3
JavaScript RegExp Object
The RegExp object is used to specify what to search for in a text
What is RegExp RegExp, is short for regular expression When you search in a text, you can use a pattern to describe what you are searching for RegExp IS this pattern ! si"ple pattern can be a single character ! "ore co"plicated pattern consists of "ore characters, and can be used for parsing, for"at chec#ing, substitution and "ore $ou can specify where in the string to search, what type of characters to search for, and "ore %efining RegExp The RegExp object is used to store the search pattern We define a RegExp object with the new #eyword The following code line defines a RegExp object called patt& with the pattern 'e'( var patt1=new RegExp("e"); When you use this RegExp object to search in a string, you will find the letter 'e' )ethods of the RegExp Object The RegExp Object has * "ethods( test+,, exec+,, and co"pile+, test+, The test+, "ethod searches a string for a specified value Returns true or false Exa"ple var patt1=new RegExp("e"); document.write(patt1.test("The best things in lie are ree")); Since there is an 'e' in the string, the output of the code above will be( true exec+, The exec+, "ethod searches a string for a specified value Returns the text of the found value -f no "atch is found, it returns null Exa"ple & var patt1=new RegExp("e"); document.write(patt1.exec("The best things in lie are ree")); Since there is an 'e' in the string, the output of the code above will be( e $ou can add a second para"eter to the RegExp object, to specify your search .or exa"ple/ if you want to find all occurrences of a character, you can use the 'g' para"eter +'global', .or a co"plete list of how to "odify your search, visit our co"plete RegExp object reference When using the 'g' para"eter, the exec+, "ethod wor#s li#e this( .inds the first occurence of 'e', and stores its position -f you run exec+, again, it starts at the stored position, and finds the next occurence of 'e', and stores its position Exa"ple 0 var patt1=new RegExp("e"!"g"); do " result=patt1.exec("The best things in lie are ree"); document.write(result); # while (result$=null) Since there is six 'e' letters in the string, the output of the code above will be( eeeeeenull co"pile+, The co"pile+, "ethod is used to change the RegExp co"pile+, can change both the search pattern, and add or re"ove the second para"eter Exa"ple var patt1=new RegExp("e"); document.write(patt1.test("The best things in lie are ree")); patt1.compile("d"); document.write(patt1.test("The best things in lie are ree")); Since there is an 'e' in the string, but not a 'd', the output of the code above will be( truealse 1o"plete RegExp Object Reference .or a co"plete reference of all the properties and "ethods that can be used with the RegExp object, go to our co"plete RegExp object reference The reference contains a brief description and exa"ples of use for each property and "ethod including the string object