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 common methods of front-end regular expressions

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail what are the common methods of front-end regular expressions. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

one。 Writing method

Write method one

/ regular expressions / modifiers

Modifier

/ I (ignore case)

/ g (full text search for all matching characters that appear)

/ m (multiline lookup)

/ gi (full text search, ignore case)

/ ig (full text search, ignore case)

Example: / a/gi looks for an in the content

Do not write modifiers default to only one match

Writing method two

Let a = new RegExp ('/ regular expression /', 'modifier')

two。 Several common methods

1.match

Method can retrieve the specified value within a string or find a match for one or more regular expressions.

What is returned

["matching content", index: 3, input: "string object", groups: undefined]

If the regexp modifier is / g, the returned object is an array

Syntax:

1.stringObject.match (str)

2.stringObject.match (regexp)

Take, for example, the an in the search string abc

'aabc' .match (' a') only matches the first 'aabc'. Match (/ a /) only matches the first one

2.replace

Used to replace some characters in a string for others, or to replace a substring that matches a regular expression

Syntax:

1.stringObject.replace ('first string found', 'modified string')

2.stringObject.replace (regexp (all substitutions for content matched by regular expressions), 'modified string')

3.search

He uses something similar to match, but he returns an index.

Syntax:

1.stringObject.match ('searchvalue')

2.stringObject.match (regexp)

4.test

Method is used to detect whether a string matches a pattern, and returns true if the string contains matching text, or false otherwise.

Syntax:

Regexp.test (stringObject)

Such as

/ a/.test ('a') # returns true/a/.test ('ac') # returns true/a/.test (' c') # returns false

5.exec

Method is used to retrieve the match of the regular expression in the string. If it matches, it returns an array containing the results, and if it does not match, it returns a null.

Regexp.exec (stringObject)

In fact, it is similar to match, but it is just to return the position you filled in.

But there's one thing.

Console.log ('c'.exec (' c')); # will report an error console.log ('c'.match (' c')); # will match c about "what are the common methods of leading regular expressions?" this is the end of the article. I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please 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

Internet Technology

Wechat

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

12
Report