Replace specific character only when within a boundary.
For example replace html entity only when enclosed inside single quotes.
Input:
<i>Hello</i> '<i>How are you</i>'
Output:
<i>Hello</i> '<i>How are you</i>'
Replace specific character only when within a boundary.
For example replace html entity only when enclosed inside single quotes.
Input:
<i>Hello</i> '<i>How are you</i>'
Output:
<i>Hello</i> '<i>How are you</i>'
You can use replace
with a callback:
var s = "<i>Hello</i> '<i>How are you</i>'";
var r = s.replace(/('[^']+')/g, function($0, $1) {
return $1.replace(/</g, '<').replace(/>/g, '>'); });
//=> <i>Hello</i> '<i>How are you</i>';
You'll need to use several regular expressions, first capture the text within single quotes, and then replace all occurrences of your character.
var input = "<i>Hello</i> '<i>How are you</i>'";
var quoted = input.match(/'.*'/)[0];
var output = quoted.replace("<", "<").replace(">", ">");
// output == "<i>Hello</i> '<i>How are you</i>'"