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

How to match, replace, and find regular expressions

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

Share

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

This article is about how regular expressions can match, replace, and find operations. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Js regular matching, finding and replacing

The RegExp object RegExp object represents regular expressions, which is a powerful tool for performing pattern matching on strings. Direct quantity syntax / pattern/attributes syntax for creating RegExp objects: newRegExp (pattern,attributes); parameter patter...

RegExp object

The RegExp object represents regular expressions and is a powerful tool for performing pattern matching on strings.

Direct quantity grammar

/ pattern/attributes

The syntax for creating a RegExp object:

New RegExp (pattern, attributes)

Parameters.

The parameter pattern is a string that specifies the pattern of the regular expression or other regular expression.

The parameter attributes is an optional string with attributes "g", "I", and "m" that are used to specify global, case-sensitive, and multiline matches, respectively. Before ECMAScript standardization, the m attribute was not supported. If pattern is a regular expression, not a string, you must omit this parameter.

Return value

A new RegExp object with the specified mode and flag. If the parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified RegExp.

If RegExp () is called as a function instead of the new operator, it behaves the same as when called with the new operator, except that when pattern is a regular expression, it only returns pattern instead of creating a new RegExp object.

Throw out

SyntaxError-if pattern is not a valid regular expression, or if attributes contains characters other than "g", "I", and "m", this exception is thrown.

TypeError-if pattern is a RegExp object, but the attributes parameter is not omitted, this exception is thrown.

Javascript code

Var string = "Hello"; var pattern = "/ good /"; [xss_clean] (string.replace (pattern,'$1')); / / $1 represents the matching string

Javascript code

Var string = "Hello"; var pattern = "/ good /"; [xss_clean] (string.match (pattern)); / / return value type: Array [array]

Javascript code

Var string = "Hello"; var pattern = "/ good /"; [xss_clean] (string.search (pattern)); / / return value type: Int [array] Thank you for reading! This is the end of this article on "how to match, replace, and find regular expressions". I hope the above content can be of some help to you, so that 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

Internet Technology

Wechat

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

12
Report