JavaScript Search character searching function

Source: Internet
Author: User

In the JavaScript search character searching [search] function There is also a indexof function that can determine whether the specified character exists.

If it is known that something is or is not in a string it can be very important. If you have an online forum that does not want people to be able to create users, including swearing, you can use the search feature to find bad-word username and reject any of their discoveries.


string search [Search] Feature
This string function regular expression, and then examine the string to see if there are any matching expressions. If there is a match, it will be sent back to the position, in a series of races to be found. If there is no match, returns-1. We will not go into very deep regular expressions, but we will show you how to find words in a string.

Search for the regular expression of the feature
The most important thing to remember when creating a regular expression is that it must be with the surrounding slash/regular expression/. As knowledge allows us to search for [search] a string, see if a common name "Alex" is inside.

<script type= "Text/javascript" >
var myregexp =/alex/;
var string1 = "Today's John went to the store and talked with Alex."
var matchPos1 = String1.search (myregexp);

if (matchPos1!=-1)
document.write ("There is a match at position" + matchPos1);
Else
document.write ("There is no match in the");


</script>

Results:

There is a match at position 45

Please note that we often just express the name "Alex". Search for features and then use that name to see if the string exists in the "Alex". The match was found and served as a contest (45 years old) and was sent back.

String Search [Search] Feature: Alternative search [Search]
Another basic tool regular expression is a pipe character "| "(It's below the key standard keyboard for retreat) You can look for alternative words/REGEXP1 | REGEXP2/. Instead of just looking for a word, we can now use the pipe character to search for multiple words in [search].

<script type= "Text/javascript"
var myregexp = /alex| john/;
var string1 = "Today's John went to the store and talked with Alex."
var matchPos1 = String1.search (MYREGEXP);

if (matchPos1!=-1)
document.write ("There is a match at position" + matchPos1);
Else
document.write ("There is no match in the");


</script>

Result is 6

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.