SoFunction
Updated on 2025-03-06

Methods for string replacement in JS

Methods for string replacement in js

Mainly used () method:
1, ("String that needs to be replaced", "New String"), this only replaces the first character found
2, (/String that needs to be replaced/g, "new string") This global replacement of the found characters

"yyyy-MM-dd-hh-mm-ss".replace("-","/")    //Result "yyyy/MM-dd-hh-mm-ss""yyyy-MM-dd-hh-mm-ss".replace(/-/g,"/")    //result"yyyy/MM/dd/hh/mm/ss"

Sometimes it is also used in API replacements, for example:
“/{id}/aa?spm=1001&articleId=123”.replace(“{id}”,“1111111”)

Replacement of strings in js

Definition and usage

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

grammar

(regexp/substr,replacement) parameter description
regexp/substr Required. RegExp object that specifies the substring or the pattern to be replaced.

Note that if the value is a string, it is used as the direct quantity text pattern to be retrieved instead of being converted to a RegExp object first.

replacement Required. A string value. Specifies a function that replaces text or generates a replacement text.

Return value

A new string is obtained after replacing the first match or all matches of regexp with replacement.

illustrate
The replace() method of stringObject performs a search and replace operation. It will look for substrings matching regexp in stringObject and replace those substrings with replacement. If regexp has the global flag g, the replace() method replaces all matching substrings. Otherwise, it only replaces the first matching substring.

replacement can be a string or a function. If it is a string, then each match will be replaced by the string. But the $ character in replacement has a specific meaning. As shown in the following table, it states that the string obtained from pattern matching will be used for replacement.

Characters Replace text
$1, $2,..., $99 Text that matches the 1st to 99th subexpressions in regexp.
$& substring that matches regexp.
$` text to the left of the matching substring.
$' The text to the right of the matching substring.
$$ Direct measurement symbol.

Note: ECMAScript v3 stipulates that the parameter replacement of the replace() method can be a function rather than a string. In this case, each match calls the function and the string it returns will be used as replacement text. The first argument to this function is a string that matches the pattern. The following parameters are strings that match the subexpression in the pattern, and can have 0 or more such parameters. The following argument is an integer declaring where the match appears in stringObject. The last parameter is stringObject itself.
Example

i, replace method

The purpose of this method is to replace all specified characters in the string and then generate a new string. After this method is called, the original string does not change. For example:

String s = “abcat”;
String s1 = (‘a',‘1');

The purpose of this code is to replace all the character a in the string s with character 1, the value of the generated new string s1 is "1bc1t", and the content of the string s does not change.

If you need to replace a specified string in the string with another string, you can use the replaceAll method, for example:

String s = “abatbac”;
String s1 = (“ba”,“12”);

The purpose of this code is to replace all the string "ab" in the string s with "12", generate a new string "a12t12c", and the content of the string s does not change.

If you only need to replace the first specified string that appears, you can use the replaceFirst method, for example:

String s = “abatbac”;
String s1 = s. replaceFirst (“ba”,“12”);

The purpose of this code is to replace only the string "," that appears globally in the string s with the string "/".

var str = "Is,is,the,cost,of,of,gasoline,going,up,up";
((/\,/g,'/'));

Use regularity to perform global matching and replace the global "," with "/".

Example 1
In this example, we will replace "Microsoft" in the string with "":

<script type="text/javascript">
var str="Visit Microsoft!"
((/Microsoft/, ""))
</script>

Output:

Visit !

Example 2
In this case, we will perform a global replacement, and whenever "Microsoft" is found, it is replaced with "":

<script type="text/javascript">
var str="Welcome to Microsoft! "
str=str + "We are proud to announce that Microsoft has "
str=str + "one of the largest Web Developers sites in the world."
((/Microsoft/g, ""))
</script>

Output:

Welcome to ! We are proud to announce that
has one of the largest Web Developers sites in the world.

Example 3
You can use the code provided in this example to ensure that the string capital characters are matched correctly:

text = "javascript Tutorial";
(/javascript/i, "JavaScript");

Example 4
In this case, we will convert "Doe, John" to "John Doe" form:

name = "Doe, John";
(/(\w+)\s*, \s*(\w+)/, "$2 $1");

Example 5
In this example, we will replace all the floral quotes with straight quotes:

name = '"a", "b"';
(/"([^"]*)"/g, "'$1'");

Example 6
In this example, we will convert the initial letters of all words in the string to uppercase:

name = 'aaa bbb ccc';
uw=(/\b\w+\b/g, function(word){
 return (0,1).toUpperCase()+(1);}
 );

This is the end of this article about the method of string replacement in js. For more related js string replacement content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!