0

I have following string

"content \" content <tag attr1=\"val1\" attr2=\"val2\">content \" content</tag> content \" content"

I want to replace all " characters inside tags definitions to ' character with use String.replace() function. " characters inside tag content must remain in its present form.

"content \" content <tag attr1='val1' attr2='val2'>content \" content</tag> content \" content"

Maybe is regex with some grouping can used?

Nawa
  • 2,058
  • 8
  • 26
  • 48

4 Answers4

1

You can use replace() and a regex-callback to do this for you:

var str = 'content " content <tag attr1="val1" attr2="val2">content " content</tag> content " content';

function replaceCallback(match) {
    return match.replace(/"/g, "'");
}

str = str.replace(/<([^>]*)>/g, replaceCallback);

The regex will match anything in-between < and > characters in your string and pass the matches to the replaceCallback() method which will then, as desired, replace " with ' characters.

Edit: The replaceCallback() was using .replace('"', "'"), but this would only replace the first ". I've updated it to use a regex-replace instead and it now works as-desired.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • it's seems right. Sorry for another question, but how to regexp will look if I want to raplace " inside definition, but not in tags with other names? – Nawa Aug 09 '12 at 16:39
  • @Nawa You can change the initial-regex to be `/]*)>/g` instead; that will match only the tags that start as ` – newfurniturey Aug 09 '12 at 16:42
  • Sorry once again. My specific case is content where I replace "<" to "[LESS_THEN]" and ">" to "[GREATER_THEN]". Content after these changes looks "content \" content [LESS_THEN]tag attr1=\"val1\" attr2=\"val2\"[GREATER_THEN]content \" content[LESS_THEN]/tag[GREATER_THEN] content \" content". I can't understand how to describe "[GREATER_THEN]" in your regexp – Nawa Aug 10 '12 at 07:41
1

The following code (taken from the accepted answer to this question) will also work, without a callback, but with a less easily understandable regex.

var str = 'content " content <tag attr1="val1" attr2="val2">content " content</tag> content " content';
str = str.replace(/"(?=[^<]*>)/g, "'");
Community
  • 1
  • 1
Sheldon Griffin
  • 4,405
  • 1
  • 14
  • 5
1
var str = "content \" content <tag attr1=\"val1\" attr2=\"val2\">content \" content</tag> content \" content";

var replaced = str.replace(/\<.*?\>/g, function(match, index){ 
  return match.replace(/"/g, "'"); // `match` will be each tag , both opening and closing. 
});
Diode
  • 24,570
  • 8
  • 40
  • 51
1

You can't do it with just one regex, you must use a combination. This is the way to go:

  1. Match the text within the tags, this is easy.
  2. Replace the characters from the output of the previous match with the ones you want using replace, this is also easy, now that you have done the previous match.

Salute you! Whatever you are.

danielrvt
  • 10,177
  • 20
  • 80
  • 121