Home > Web Front-end > JS Tutorial > body text

Examples of exec usage in js regular expressions_javascript skills

WBOY
Release: 2016-05-16 15:49:13
Original
1571 people have browsed it

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);
}

Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template