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 js determine whether a string contains a character?

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "js how to judge whether a string contains a character", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "js how to determine whether a string contains a character" bar!

1. Use the string search () method

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

Var string = "foo", expr = / oo/;string.search (expr); / / location is 1

2. Use the string match () method

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

Var string = "foo", expr = / oo/;string.match (expr); / / ["oo", index: 1, input: "foo", groups: undefined]

3. Use the regular test () method

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

Let str= "abc" let reg=RegExp (/ b /) console.log (reg.test (str)) / / true

4. Use the regular exec () method

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 groupName= "Xiaobai Group A"; var reg = RegExp (/ group /); alert ('reg.exec (groupName) =' + (reg.exec (groupName) / / Group Thank you for your reading. The above is the content of "how js determines whether a string contains a character". After the study of this article, I believe you have a deeper understanding of how js determines whether a string contains a character, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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