Replacing Patterns with New Strings - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Replacing Patterns with New Strings - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Saturday, December 22, 2018

Replacing Patterns with New Strings

Replacing Patterns with New Strings


You want to replace all matched substrings with a new substring. 


Use the String’s replace() method, with a regular expression:

var searchString = "Now is the time, this is the tame"; 
var re = /t\w{2}e/g; 
var replacement = searchString.replace(re, "place"); 
console.log(replacement); // Now is the place, this is the place 


The solution also makes use of a global search. Using the global flag (g) with the regular expression in combination with the String replace() method will replace all instances of the matched text with the replacement string. If we didn’t use the global flag, only the first match would trigger a replacement. 

The literal regular expression begins and ends with a slash (/). As an alternative, I could have used the built-in RegExp object: 

var re = new RegExp('t\\w{2}e',"g"); 
var replacement = searchString.replace(re,"place"); 

The difference is the surrounding slashes aren’t necessary when using RegExp, but the use of the backslash in the pattern has to be escaped. In addition, the global indicator is a second, optional argument to the RegExp constructor.

 You can use a regular expression literal or a RegExp object instance interchangeably. The primary difference is that the RegExp constructor allows you to create the regular expression dynamically.

Regular Expression Quick Primer

Regular expressions are made up of characters that are used alone or in combination with special characters. For instance, the following is a regular expression for a pattern that matches against a string that contains the word technology and the word book, in that order, and separated by one or more whitespace characters:

var re = /technology\s+book/;

The backslash character (\) serves two purposes: either it’s used with a regular character, to designate that it’s a special character, or it’s used with a special character, such as the plus sign (+), to designate that the character should be treated literally. 

In this case, the backslash is used with s, which transforms the letter s to a special character designating a whitespace character (space, tab, line feed, or form feed). The \s special character is followed by the plus sign, \s+, which is a signal to match the preceding character (in this example, a whitespace character) one or more times. This regular expression would work with the following:

technology book 

It would also work with the following:
technology book 

It would not work with the following, because there is no white space between the words:


It doesn’t matter how much whitespace is between technology and book, because of the use of \s+. However, using the plus sign does require at least one whitespace character.

No comments:

Post a Comment

Post Top Ad