Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does js determine whether a string contains a character?

Shulou Source: shulou.com Published: 2022-05-31 10:39:00 09月17日 Update

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!

Tags: Characters strings methods regular retrieval expression learning middle finger content location available in multiple that is ideas situations arrays articles more knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Redmi Microsoft Huawei