![]() Suppose you have a string like this: backgroundColorĪnd you want to transform it into something like: background-color Let’s take an example of using the replace() method with a replacer function. groups is an object whose are are the named capturing group and values are matched values.offset is an integer that specifies the offset of the matched substring within the input string.p1, p2, … the values of the capturing groups in the regexp.The replacerFunction has the following arguments: If the regexp uses the global flag ( g), the replace() method will call the replacerFunction after every match. The replacerFunction is used to create a substring to replace the match. The replace() method calls the replacerFunction after it finds the first match. The second argument of the replace() method can be a function like this: replace(regexp, replacerFunction) JavaScript regex replace() method with replacer function To swap the first name and last name, we place the second match ( $2) first and then the first match ( $1). In the newSubstr, we use $1 to reference the first capturing group and $2 to reference the second one. The first capturing group captures the first word and the second one captures the second word after the space. The regular expression contains two capturing groups. In other words, it matches any string that has a word, space, and another word. The regular expression /(\w )\s(\w )/ matches one or more word characters, a space, and then one or more word characters. Let lastFirst = name.replace(re, '$2, $1') The following example illustrates how to use the replace() method with capturing groups to swap the first and last names in a person name: let re = /(\w )\s(\w )/ For example, $1 and $2 reference first and second capturing groups. When a regular expression contains the capturing groups, you can reference these groups in the newSubstr using the $N syntax where N is the grouping number. Output: JavaScript and JavaScript 3) Using the JavaScript regex replace() method with capturing groups The following example uses the replace() method with a regular expression containing a global flag ( g) to replace all matches: const s = 'JS and js' ![]() 2) Using the JavaScript regex replace() method with the global flag To replace all matches, you use the global flag ( g) in the regular expression. However, the replace() method replaces only the first match ( JS). The /js/i matches both JS and js in the 'JS and js' string. The following example uses the replace() method to replace the first match of the JS string with the JavaScript string: const s = 'JS and js' Ĭonst newS = s.replace(re, 'JavaScript') Ĭonsole.log(newS) Code language: JavaScript ( javascript ) 1) A simple the JavaScript regex replace() method example Let’s take some examples of using the replace() method. JavaScript regex replace() method examples To replace all matches, you use the global flag ( g) in the regexp. Note that the replace() method doesn’t change the original string but returns a new string.īy default, the replace() method replaces the first match if the regexp doesn’t use the global flag ( g). The replace() returns a new string with the matches replaced by the newSubstr. If the newSubstr is empty, the replace() method removes the matches. The newSubstr is a string to replace the matches.The regexp is a regular expression to match.The following shows the syntax of the replace() method: replace(regexp, newSubstr) This tutorial focuses solely on regular expressions. The () method works with both strings and regular expressions. Introduction to the JavaScript replace() method Summary: in this tutorial, you’ll learn how to use the string replace() method to return a new string with some or all matches of a regular expression replaced by a replacement string.
0 Comments
Leave a Reply. |