Starting string:
I like [dogs], [cats], and [birds]
Final output needed:
I like <a href="#">dogs</a>, <a href="#">cats</a>, and <a href="#">birds</a>
So basically changing items with brackets to links.
Find and replace multiple values with nested SUBSTITUTE The easiest way to find and replace multiple entries in Excel is by using the SUBSTITUTE function. The formula's logic is very simple: you write a few individual functions to replace an old value with a new one.
To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag. replaceAll() method is more straight forward.
01) Using replace() method Python offers replace() method to deal with replacing characters (single or multiple) in a string. The replace method returns a new object (string) replacing specified fields (characters) with new values.
Using the REPLACE() function will allow you to change a single character or multiple values within a string, whether working to SELECT or UPDATE data.
Use this expression:
var str = 'I like [dogs], [cats], and [birds]';
alert(str.replace(/\[(.+?)\]/g, '<a href="#">$1</a>'));
\[(.+?)\]
asks for a literal [
, to lazily match and capture anything, then to match a literal ]
. Replace with the captured stuff enclosed in <a>
tags.
The g
modifier means global replacement, i.e. find and replace every match and not just the first.
jsFiddle preview
It's a simple string replace.
function tagIt(source)
{
return source.replace('[', '<a href="#">').replace(']', '</a>');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With