JS Implementation ReplaceAll

Source: Internet
Author: User

Need to replace multiple double-quotes inside the string, no nonsense, directly on the code:

var filePath = ' D:/IMG/1.JGP '; filePath = Filepath.replace (New RegExp (/(")/g)," ");
Results after substitution:
<pre name= "Code" class= "JavaScript" >D:/IMG/1.JGP

The Replace function is also available:

JavaScript Replace () method

JavaScript String Object

Defining and Using methods

The replace () method is used to replace some characters in a string with some characters, or to replace a substring that matches a regular table.

Grammar
Stringobject.replace (regexp/substr,replacement)
number of references Descriptive narrative
regexp/substr

required. The

Specifies the substring or RegExp object of the pattern to replace.

Notice that the value is assumed to be a string. It is used as the direct volume text pattern to be retrieved, instead of being converted to the RegExp object first.

Replacement Necessary. A string value. A function that specifies replacement text or generates alternate text.

return value

A new string that is obtained after the first match or all matches of regexp have been replaced with replacement .

Description

The replace () method of the string Stringobject runs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement . Assume that RegExp has the global flag G. Then the replace () method replaces all of the matched substrings. Otherwise. It simply replaces the first matching substring.

A replacement can be a string, or it can be a function. Suppose it is a string. Then each match will be replaced by a string.

However, the $ character in replacement has a specific meaning. For example, see the following table. It shows that the resulting string from the pattern match will be used for substitution.

character Replace text
$, $ 、...、 The text that matches the 1th to 99th sub-expression in RegExp.
$& A substring that matches the regexp.
$` The text at the left side of the matching substring.

$ The text at the right side of the matching substring.

$$ Direct volume symbol.

Note: ECMAScript v3 stipulates that the parameter of the replace () method replacement can be a function rather than a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next parameter is a string that matches the subexpression in the pattern and can have 0 or more of these parameters.

The next parameter is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.

Example Sample 1

In this example. We will replace "Microsoft" in the string with "W3school":

<script type= "Text/javascript" >var str= "Visit microsoft!" document.write ( str.replace(/Microsoft/, "W3School") ) </script>

Output:

Visit w3school!
Example 2

In this case, we will run a global substitution. Whenever "Microsoft" is found. It is replaced with "W3school":

<script type= "Text/javascript" >var str= "Welcome to microsoft! "Str=str +" We are proud to announce that Microsoft have "Str=str +" one of the largest WEB developers sites in the world. " document.write ( str.replace(/Microsoft/g, "W3School") ) </script>

Output:

Welcome to w3school! We is proud to announce this W3schoolhas one of the largest WEB developers sites in the world.
Example 3

You can use the code provided in this example to ensure the correct matching string uppercase characters:

Text = "JavaScript Tutorial"; Text.replace (/javascript/i, "JavaScript");
Example 4

In this example. We will convert "Doe, John" to the form of "John Doe":

Name = "Doe, John"; Name.replace (/(\w+) \s*, \s* (\w+)/, "$ $");
Example 5

In this example, we will replace all the flower quotes with the direct arguments:

name = ' A ', "B" '; Name.replace (/"([^"]*) "/g," ' $ ' ");
Example 6

In this example. We will convert all of the words in the string to uppercase in the first letter:

Name = ' aaa bbb ' CCC '; Uw=name.replace (/\b\w+\b/g, function (word) {  return word.substring (0,1). toUpperCase () + Word.substring (1);}  );
Tiy
Replace () 1
How to replace characters in a string with replace ().

Replace () 2-Global Search
How to use replace () for global substitution.
Replace () 3-non-sensitive search for uppercase and lowercase
How to use replace () to ensure that uppercase letters are correct.
Replace () 4
How to use replace () to convert the name format.
Replace () 5
How to use replace () to convert an argument.
Replace () 6
How to use replace () to convert the first letter of a word to uppercase.


JS Implementation ReplaceAll

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.