Home  >  Article  >  Web Front-end  >  Examples of exec usage in js regular expressions_javascript skills

Examples of exec usage in js regular expressions_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:49:131529browse

The example in this article describes the usage of exec in js regular expressions. Share it with everyone for your reference. The details are as follows:

Pay attention to the following points with exec:

1. exec returns an array
2. The attributes of this array include input (the entire string being matched) index (the starting position of the first element matched)
3. lastIndex also has a position pointed to after matching the first attribute. This attribute is intelligently accessed by the RegExp object!!!
4. You can use this attribute to get the string of matched characters

The code is as follows:

function o_exec(){
  var str="hjjh,catfff,dog,catarigy,catdog,hjfkhj";
  var reg=/cat\S*?\b/g;
  arr=reg.exec(str);
  while(reg.lastIndex!=str.length){
    alert(arr[0]);
    arr=reg.exec(str);
}

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn