The "front-end Learning" string Replace uses

Source: Internet
Author: User

The Replace method of a String object contains two parameters, the first parameter represents a regular expression that performs a match, a string is passed, and the second parameter represents a substring in place of a match.

   var B = s.replace ("str1", "str2");

Unlike the search and match methods, the Replace method does not convert the string to a regular expression object, but rather matches it with the literal pattern of the direct amount of the string. The second argument can be a replacement text, or a function that generates alternate text, and returns the value as the replacement text.

The Replace method performs both a find and a replace of two operations. The method finds substrings in the string that match the regular expression, and then calls the second argument to replace the substrings. There are two ways to use the expression in JS, one is the method of the regular expression object, the other is the method of the string object, the former has the Exec (str), Test (str) Two methods, the latter has match (regexp), replace (regexp), Search (RegExp), split (search) four methods. If the regular expression has a global nature, all matching substrings are replaced, or only the first matched substring is replaced.

   var b = ' 1231231234 ';

Console.log (B.replace (' 123 ', ' 321 '))// 3211231234

Console.log (B.replace (/123/, ' 321 '))//3211231234 non-global regular

Console.log (B.replace (/123/g, ' 321 '))//3213213214 Global Regular

A special character "$" is contracted in the Replace method, and if an ordinal is added, it represents a string stored in a reference to a matching subexpression in the regular expression. For example:

   var s = "JavaScript";

var B = s.replace (/(Java) (script)/, ' $2-$1 ');

Console.log (b)//Script-java

    • $1,$2...$99; The text that matches the 1th to 99th sub-expression in a regular expression.
    • $& substring that matches the regular expression.
    • $ ' text located to the left of the matched substring
    • $ ' text on the right side of the matched substring
    • $$ represents $

When the second argument uses a function

var s = ' script language = ' javascript ' type = ' text/javascript ';

var f = function ($) {

Return $1.substring (0,1). toUpperCase () + $1.substring (1)

};

Console.log (S.replace (/(\b\w+\b)/g,f)); Script Language = "Javascript" Type = "Text/javascript"

var F2 = function ($1,$2,$3) {

Return $2+$3

};

Console.log (S.replace (/(\b\w+\b)/g,f2)); script0 Language7 = "javascript19" type31 = "text40/javascript47"

    • Arguments[0] Each match of text
    • Arguments[1]~argument[n-3] First to last match sub-expression match text
    • Arguments[n-2] matches the subscript of the text
    • ARGUMENTS[N-1] executing a matching string

Front-end learning string replace using

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.