Js string SEARCH examples

Source: Internet
Author: User

There are many methods to search for characters in js. One is to search for a specific string and return a string, and the other is to find the position of the string. I will share with you their implementation methods.


Searches for a specific character in a string. If yes, this character is returned.

Example 1 match () function

The Code is as follows: Copy code

<Script type = "text/javascript">
Var str = "heyanping"
Document. write (str. match ("ing") + "<br/>") // The result is ing, because the "heyanping" string contains ing
Document. write (str. match ("img") + "<br/>") // The result is null because the "heyanping" string does not contain img
</Script>

Example 2

IndexOf can return the position of a specified string. This function returns an integer. Note: The characters in javascript are counted from 0. Below is a simple example:

The Code is as follows: Copy code


<Script type = "text/javascript">

Var my_str = "Welcome to www. bKjia. c0m"

Document. write (my_str.indexOf ("share "))

</Script>

The Return Value of the code above is 15.

LastIndexOf function:

The lastIndexOf function is also used to return the position of a string. Unlike indexOf, it searches forward from the end of a string and returns the position at which the string appears, through this function, we can find the final position of the specified string.

The following is an example:

The Code is as follows: Copy code

Var my_str = "Welcome to www. bKjia. c0m"

Document. write (my_str.lastIndexOf ("o "))

The output result of the above Code is 24, that is, the position where the last o appears.

Count the number of occurrences of characters in a string

Let's take a look at the Code provided by cloudchen that uses regular expressions:

The Code is as follows: Copy code

<Script language = "JavaScript">
Var str = "cloudchen"; var find = "c ";
Var reg = new RegExp (find, "g ")
Var c = str. match (reg );
Alert (c? C. length: 0) </SCRIPT>
Var reg = new RegExp (find, "g ")

A regular expression can be written as follows:

Var reg =/c/g; c indicates the character to be matched, where g indicates global search. Match returns the matched characters (strings ). In this way, the number of occurrences of the specified character in the string is obtained. This is a very formal practice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.