Home  >  Article  >  Web Front-end  >  Example of how JavaScript uses regular expressions to obtain all grouped contents

Example of how JavaScript uses regular expressions to obtain all grouped contents

高洛峰
高洛峰Original
2017-01-20 14:00:111274browse

The example in this article describes how JavaScript uses regular expressions to obtain all grouped contents. Share it with everyone for your reference, the details are as follows:

1. Need to use regular expression exec

2. Need to loop

DEMO example: (The following code will output 8 , 9. Two matched group contents)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS正则demo</title>
</head>
<body>
<script>
var r = /\{ab(\d)ab}/g;
while ((resultS2 = r.exec("ab{ab8ab}c{ab9ab}")) != null) {
  console.log(resultS2[1]);
} 
</script>
</body>
</html>

The running effect diagram is as follows:

Example of how JavaScript uses regular expressions to obtain all grouped contents

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

For more examples of how JavaScript uses regular expressions to obtain all grouped content, please pay attention to the PHP Chinese website for related articles!

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