Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the properties of the JavaScript RegExp object

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what attributes the JavaScript RegExp object has, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The JavaScript RegExp object has five properties, namely: global, ignoreCase, multiline, source, and lastIndex. Global, ignoreCase, and multiline are used to detect whether the regular expression has a g flag, an I flag, and an m flag. If the flag is set, it returns true, otherwise it returns false.

What are the properties of the JavaScript RegExp object

For example, detect whether a regular expression has modifiers:

Alert (/ abc/g.global); / / display true

Alert (/ ^ abc$/.global); / / Show false

Alert (/ ^ [0-9a-z] {1,} $/ .roomreCase); / / display false

Alert (/ ^ [0-9a-z] {1,} $/ i.ignoreCase); / / display true

The source property is used to return the source text of the regular expression, which does not include the delimiters used by the literal amount of the regular expression, nor does it include the flags g, I, m.

For example:

Alert (/ abc/.source); / / display abc

Alert (/ ^ abc$/.source); / / Show ^ abc$

Alert (/ www.itxueyuan.org/ig.source); / / display www.itxueyuan.org

Alert (/ ^ [0-9a-z] {1,} $/ i.source); / / display ^ [0-9a-z] {1,} $

The lastIndex attribute is used to specify the starting position of the next match.

The lastIndex attribute holds an integer that specifies the starting position of the next match, which is the position of the first character after the last successful text.

What are the properties of the JavaScript RegExp object

Let's look at the following example:

Var str= "Itxueyuan's domain is www.itxueyuan.org.Welcome to itxueyuan!"

Var pattern=new RegExp ("itxueyuan", "ig")

Var iTunes 1

While (pattern.test (str)) {

ITunes +

Alert (starting position of "th" + I + "second match:" + pattern.lastIndex)

}

Running result:

Starting position of the second match: 9

Starting position of the third match: 35

Starting position of the 4th match: 61

The above example shows that the test () method takes the location indicated by the lastIndex attribute as the starting point for the next retrieval. The first search, the second search of lastIndex=0;, the third search of lastIndex=9;, the fourth search of lastIndex=35;, lastIndex=61.

In this way, you can iterate through all the matching text in a string by repeatedly calling the test () method.

The lastIndex property is readable and writable and can be set as soon as the next search for the target string begins. When the test () method can no longer find matching text, it automatically resets the lastIndex property to 0.

Note:

RegExp objects that do not have a flag g and do not represent global mode cannot use the lastIndex attribute.

If you start retrieving a new string after successfully matching a string, you need to manually set this property to 0.

About what attributes of the JavaScript RegExp object to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report