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 use exec, test, match, search, replace and split in js

2025-04-06 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 how to use exec, test, match, search, replace and split in js. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Exec: regularizes the string and returns the matching result. Array [0] is the original string and array [I] is the position in the entire searched string.

Test: test whether the string contains matching results, including the returned true, but not the returned false.

Match (pattern): regular matching is performed according to pattern. If the match is reached, the match result is returned. If the match is not reached, null is returned.

Search (pattern): regular matching is performed according to pattern. If a result is matched, its index number is returned; otherwise,-1 is returned.

Replace (pattern,replacement): perform regular matching according to pattern and replace the matching result with replacement

Split (pattern): regular segmentation based on pattern and returns an array of partitions

Example:

The copy code is as follows:

Function checkForm () {

Var u = document.form_name.check.value

Var s = / ^ [- a-zA-Z0-9 _] + (\. [- a-zA-Z0-9 _] +) * @ [- a-zA-Z0-9 _] + (\. [- a-zA-Z0-9 _] +) * $/

Var a = s.exec (u)

Var a = s.test (u)

Var a = u.match (s)

Var a = u.search (s)

Alert (a)

}

Replace

The simplest ability is simple character substitution. The sample code is as follows:

The copy code is as follows:

Var u = "javascript is a good script language"

/ / here I would like to replace the letter a with the letter A.

Var s = / a _ Unig

Var a = u.replace (s, "A")

Split

The copy code is as follows:

Var str= "How are you doing today?"

[xss_clean] (str.split ("") + "")

[xss_clean] (str.split ("") + "")

[xss_clean] (str.split (", 3))

The output is as follows:

How,are,you,doing,today?

H,o,w, a,r,e, y,o,u, d,o,i,n,g, t,o,d,a,y,?

How,are,you

This is the end of the article on "how to use exec, test, match, search, replace and split in js". 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, 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: 261

*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