Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the common methods of front-end regular expressions

Shulou Source: shulou.com Published: 2022-06-02 05:21:49 09月28日 Update

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.

Tags: Character string regular expression method content grammar commonly used full text writing size article front end object that is array more retrieval good practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn Shulou Tech Info Huawei Linux