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 does javascript determine whether it contains a specified string

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to determine whether javascript contains a specified string, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Judgment method: 1, use the indexOf () method to get the first occurrence position of the specified string value, if the return value is "- 1", it does not contain; 2, use the search () method to retrieve the specified string, if the return value is "- 1", it does not contain; 3, use the match () method; 4, use the test () method, and so on.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

JavaScript determines whether a string contains a specified string

Method 1: indexOf () (recommended)

The indexOf () method returns the position where a specified string value first appears in the string. If the string value to be retrieved does not appear, the method returns-1.

Var str = "123"; console.log (str.indexOf (" 3 ")! =-1); / / true

Method 2: search ()

The search () method is used to retrieve a specified substring in a string, or to retrieve a substring that matches a regular expression, and returns-1 if no matching substring is found.

Var str= "123"; console.log (str.search ("3")! =-1); / / true

Method 3: match ()

The match () method retrieves the specified value within a string or finds a match of one or more regular expressions.

Var str= "123"; var reg=RegExp (/ 3 /); if (str.match (reg)) {/ / include}

Method 4: test ()

The test () method is used to retrieve the value specified in the string. Return true and false.

Var str= "123"; var reg=RegExp (/ 3 /); console.log (reg.test (str)); / / true

Method 5: exec ()

The exec method is used to retrieve the match of a regular expression in a string. Returns an array containing the matching results. If no match is found, the return value is null.

Var str= "123"; reg=RegExp (/ 3 /); if (reg.exec (str)) {/ / contains} JavaScript is the abbreviation of JavaScript, it is a literal scripting language, its interpreter is called JavaScript engine, is a part of the browser, mainly used for web development, can add a variety of dynamic effects to the website, make the page more beautiful.

This is how javascript determines whether it contains a specified string. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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