code Python module
| COM PHP module
| Ev PHP module
| http Python module
| io Python module
| os Python module
| Python functions
| re Python module
| Regular Expressions
| stat Python module
| string Python module
| Strings PHP module
| UI PHP module
👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
replace () function takes two arguments, the substring to replace and the new string that will take its place. Regex (p) can also be used to replace text in a string.
Replacing text in a string is a common operation in any programming language. For example, you might want to remove some symbols from a string to make sure your program is working.
The replace () method takes two arguments:
- The pattern or regular expression to search for replace (); and
- A string that should replace any instance of the pattern specified in the first argument.
Let’s take a look at this syntax:
Our first argument, "Pi", is the pattern whose contents we want to replace. The second argument, "P", is the text we want to replace our template with. So our code replaces "Pi" with "P".
The replace () method is added at the end of our chain. You don’t need to specify the string from which you want to replace the values. Only the pattern that you want to find and the text that should replace instances of that pattern are accepted arguments.
For example, suppose you want to replace the interesting word by intriguing in a string. How would you do? We could use the replace () method:
Our code returns a new string which is as follows:
We can see that the word "interesting" has been changed to "intriguing" in our channel.
If we want to replace multiple parts of our string, we can do so by concatenating our replace () functions. Chaining means that we place several replace () statements one after the other. Here is an example:
Our code returns the following:
Our code replaces all occurrences of the words" This "," string " and" interesting ", with the words" Our ‚"," code ‚"," amazing! ‚", respectively. It then replaces three words with three more in our string using the replace () function.
Regular Expression, or regex (p), defines a search pattern. When trying to find or replace characters in a string, using regular expressions can be helpful. The regexp objects give us more control over our replace () function, which allows us to do more interesting replacements.
Here is an example of using regex to replace the letter and with the letter f:
Our code output is as follows:
Our code first declares the variable ourInterestingString and assigns it the value This string is interesting . Next, our code replaces each e in the entire string with the letter f and the function returns our new string.
We use the regular expression / and / g / to do this. This pattern will replace any and appearing in the entire string (which is the meaning of g).
If you’re looking to learn more about regular expressions, check out Regexr , which allows you to easily try different expressions.