Use the replace method in js to convert a character.

Source: Internet
Author: User

Use the replace method in js to convert a character.

Copy codeThe Code is as follows:
Var str = "Guo Zi [1990] No. 12 ";

How to add a backslash before "[" and "]" in the variable,

To replace it with this: str = "Guo Zi \ [1990 \] No. 12 ";
Copy codeThe Code is as follows:
<Script language = "javascript" type = "text/javascript">
Var str = "Guo Zi [1990] No. 12 ";
Str = str. replace (/\ [/g ,'\\[');
Str = str. replace (/\]/g, '\]');
Document. write (str );
</Script>


How can I replace all the target strings in js?

The string replacement function provided by js can replace the matching strings in the string with the target string. However, you can only Replace the first matching result by searching and replacing the string. For example
Var str = Are all men? Isn't it !;
Str = str. replace ');
Alert (str );
Var str = Are all men? Isn't it !; Str = str. replace ('?', '?'); alert (str );
In the above Code, only the first one is replaced. The output result is that everyone is a man, isn't it! , Hey
If you want to replace all the content in the string that meets the conditions, it is good to replace it with a regular expression. The following code can replace all
Var str = 'are all men? Isn't it? ';
Str = str. replace (/?/g, well ');
Alert (str );
Var str = 'are all men? Isn't it? '; Str = str. replace (/?/g,' Well '); alert (str );
In this way, we will output that everyone is a man, isn't it ?, All the content that meets the condition is replaced. Note that the regular expression/?/g indicates the g here. This g indicates global, and this configuration item will not be completely replaced.
Of course, through complex regular expressions, js replace can also achieve richer string replacement processing results.

How can I replace all the target strings in js?

The string replacement function provided by js can replace the matching strings in the string with the target string. However, you can only Replace the first matching result by searching and replacing the string. For example, var str = Are all men, isn't it !; Str = str. replace ('?', '?'); alert (str); var str = Are all men !; Str = str. replace ('?', '?'); alert (str); in the above Code, only the first one is replaced. The output result is that everyone is a man, isn't it! Hey hey, if you want to replace all the qualified content in the string, it is a good way to replace it with a regular expression. The following code can replace all var str = 'are all men, isn't it? '; Str = str. replace (/g,' Well '); alert (str); var str =' are all men, aren't they? '; Str = str. replace (/g,' Well '); alert (str); this will output that everyone is a man. Isn't it ?, All the content that meets the condition is replaced. Note that the regular expression/?/g indicates the g here. This g indicates global, and this configuration item will not be completely replaced. Of course, through complex regular expressions, js replace can also achieve richer string replacement processing results.

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.