Search method of JS regular expression

Time:2020-10-31

Function: returns the position of the first substring that matches the regular expression lookup

Grammar: stringObj.search (rgexp) stringobj required rgexp regular expression

Return value: the search method indicates whether there is a corresponding match. If a match is found, the search method returns an integer value indicating the offset from the beginning of the string. If no match is found, – 1 is returned

Example code:

Copy codeThe code is as follows:
<html>
<script language=”javascript” type=”text/javascript”>
//The search method indicates whether a corresponding match exists. If a match is found, the search method returns an integer value indicating the offset from the beginning of the string. If no match is found, – 1 is returned
Var re = / (/ D) (/ D) / D / 2 / 1 /; / / set regular expression
Var Ostr = 11010111 “; / / the string to be matched. The first position of the string starts from 0
var pos= ostr.search (RE); / / string matching
If (POS = = – 1) {/ / if no match is found
document.write (“no match found”);
}
else{
arr= ostr.match (RE); / / match to find the matching content
document.write (“found the first match in” + POS + “, the matching content is:);
document.write (arr [0]); / / output the matching content
}
</script>
</html>

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]