Examples of common JavaScript regular functions and javascript examples

Source: Internet
Author: User

Examples of common JavaScript regular functions and javascript examples

This example describes common regular functions in JavaScript. We will share this with you for your reference. The details are as follows:

Use JavaScript regular functions such as match, exec, test, search, replace, and split.

* The attributes "g", "I", and "m" are used to specify global matching, case-sensitive matching, and Multiline matching, respectively.

1. match () function

Match (): If no match is found, the return value is null. If a match is found, a result array is returned.

Function RegExpMatch () {var str = "http://www.bkjia.com/jiaoben"; var reg =/[j] [\ s \ S] {3}/gi; // note that g will match the full text, if this parameter is not added, only the first matching var result = str will be returned. match (reg); alert (result); // return: jb51, jiao}

2. exec () function

Exec () function: If no match is found, the return value is null. If a match is found, a result array is returned.

The first element of this array is the text that matches the regular expression, and the second element matches the text of the second subexpression of RegExpObject (if any ), the 2nd element is the text (if any) that matches the 2nd sub-expressions of RegExpObject.

Function RegExpExec () {var str = "1234-5678"; var reg =/(\ d {4})-(\ d {4 })/; var result = reg.exe c (str); alert (result); // return: 1234-5678,1234, 5678 alert (result [1] + ''+ result [2]); // 1234 5678 alert (RegExp. $1 + ''+ RegExp. $2); // 1234 5678}

3. test () function

Test () function: returns a Boolean value indicating whether the string to be searched matches the given regular expression.

Function RegExpTest () {var str = "http://www.bkjia.com/jiaoben"; var reg =/^ http: \/([\ w-] + \.) + [\ w-] + (\/[\ w-] *)? $ | ^ ([\ W-] + \.) + [\ w-] + (\/[\ w-] *)? $ // Verify URL format var result = false; if (reg. test (str) {result = true;} alert (result); // true}

4. search () Functions

Search () function: returns the position of the first substring that matches the content found in the regular expression. If no match is found, the return value is-1.

function RegExpSearch() {  var str = "http://www.bkjia.com/jiaoben";  var reg = /(jiaoben)/;  var result = str.search(reg);  alert(result);  //20}

5. replace () function

Replace () function: returns the string copy after text replacement based on the regular expression.

function RegExpReplace() {  var str = "http://www.bkjia.com/jiaoben";  var reg = /^(http:\/\/www.jb51.net)\/([\w]*)$/;  var result = str.replace(reg, "$1?userId=$2");  alert(result);  //http://www.bkjia.com?userId=jiaoben}

6. split () function

Split () function: Splits a string into substrings and returns the result as a string array.

function RegExpSplit() {  var str = "1@4@7@9";  var reg = /@/;  var result = str.split(reg); ;  alert(result);  //[1,4,7,9]}

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

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.