Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Does javascript replace () need to be case sensitive?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "does javascript replace () need to be case-sensitive?" interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "does javascript replace () need to be case-sensitive?"

In javascript, the replace () function can use regular expressions to replace strings that are case-insensitive, with the syntax "string.replace (/ value to be looked up / gi," replacement value ")", where "g" represents global substitution and "I" ignores case.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, the replace () function can replace strings that are case-insensitive, with the help of regular expressions.

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

Syntax:

The string.replace (searchvalue,newvalue) parameter describes that the searchvalue must. A RegExp object that specifies the substring or the schema to replace.

Note that if the value is a string, it is treated as an immediate amount of text pattern to retrieve, rather than being first converted to a RegExp object. Newvalue is required. A string value. Specifies the function that replaces the text or generates the alternate text.

Return value: a new string, obtained after replacing the first match or all matches of regexp with replacement.

The second parameter of the replace () method can use the function, which is called when the match occurs, the return value of the function is used as the replacement text, and the function can receive special characters prefixed with $to reference information about the matching text.

The special character convention string in the second parameter of the replace () method describes $1, $2,..., $99 text matching the 1st through 99th subexpression in the regular expression $& (dollar sign + hyphen) substring $'(dollar sign + switch skill key) matching regular expression $'(dollar sign + single quotation mark) text $'(dollar sign + single quotation mark) to the left of the matching substring $$represents the $string to the right of the matching string

Example 1

Replace the character a (case-insensitive) in the string with x

Replace the character a (case-insensitive) in the string with x

Var sText = "abcdefaABC"; / / g for global replacement I for ignoring case var txt = sText.replace (/ a/gi,'x'); document.getElementById ("demo") [xss_clean] = txt

Output result:

Example 2

The following code converts each word in the string to uppercase display.

Var s = 'javascript is script, is not java.'; / define string / / define replacement text function, argument to the first subexpression matching text var f = function ($1) {/ / convert the first letter of the matching text to uppercase return $1.substring (0Magazine 1). ToUpperCase () + $1.substring (1). ToLowerCase ();} var a = s.replace (/ (\ b\ w+\ b) / g, f) / / match the text and replace console.log (a); / / return the string "JavaScript Is Script, Is Not Java."

In the above example, the argument to the replacement function is the special character "$1", which represents the text matched by the regular expression / (\ b\ w+\ b) / parentheses, and then processes the matching text within the function structure, truncating its first letter and converting it to uppercase, the rest of the characters are all lowercase, and then return the newly processed string. The replace () method replaces each matching substring in the original text with the new string returned.

Example 3

The above example can be extended further, using parentheses to get more matching information. For example, if you directly use parentheses to pass the first letter of a word, and then convert it to case, the result is the same.

Var s = 'javascript is script, is not java.'; / / defines the string var f = function ($1 2.toUpperCase () + $3) {/ / defines the replacement text function, please note the change in parameters return $text () + $3;} var a = s.replace (/ (\ b\ w+\ b) / g, f); console.log (a)

In the function f (), the first parameter represents the text that matches each time, the second parameter represents the text matched by the subexpression of the first parenthesis, that is, the first letter of the word, and the second parameter represents the text matched by the subexpression of the second parenthesis.

The second parameter of the replace () method is a function, and the replace () method passes it multiple arguments, all of which have a certain meaning, as shown below:

The first parameter represents the text that matches the matching pattern, such as the string of words that match each time in the above example.

The following parameters are strings that match the sub-expressions of the matching pattern, and the number of parameters is unlimited, depending on the number of sub-expressions.

The following parameter is an integer that represents the subscript position of the matching text in the string.

The last parameter represents the string itself.

Example 4

Change the replacement text function in the above example to the following form.

Var f = function () {return arguments [1] .toUpperCase () + arguments [2];}

That is, if you do not pass formal parameters to the function, the arguments property of the function that is called directly can also read the information about the matching text in the regular expression. Where:

Arguments [0]: represents the text that matches each time, that is, the word.

Arguments [1]: represents the text that the first subexpression matches, that is, the first letter of the word.

Arguments [2]: represents the text that the second subexpression matches, that is, the remaining letters of the word.

Arguments [3]: indicates the subscript position of the matching text, for example, the subscript position of the first matching word "javascript" is 0, and so on.

Arguments [4]: indicates the string to be matched, here it means "javascript is script, is not java."

Example 5

The following code uses the arguments object of the function to actively obtain the details matched by the regular expression in the first parameter of the replace () method.

Var s = 'javascript is script, is not java.'; / / definition string var f = function () {for (var I = 0; I < arguments.length; iTunes +) {console.log (value of "th" (I + 1) + "parameter:" + arguments [I]);} console.log ("-") } var a = s.replace (/ (\ b\ w+\ b) / g, f)

In the function structure, when you use the for loop structure to traverse the arguments property, five prompts pop up each time you find a matching word, displaying the matching text information listed above. Among them, arguments [1] and arguments [2] will display the subexpression matching information of the current matching text according to each matching text, and arguments [3] will show the subscript position of the current matching word. Arguments [0] always displays the word that matches each time, and arguments [4] always shows the string being manipulated.

At this point, I believe you have a deeper understanding of "javascript replace () need to be case-sensitive". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report