wyc
wyc

Reputation: 55273

JavaScript: regex for replacing double curly quotes fails when single ones are present

Input:

"Hello."
"'I'm not.'"

Regex:

/"\b/g // opening quote 
/"\B/g // closing quote

Output:

“Hello.”

”'I'm not.'”

As you can see, the regex works OK when it's just a word. The problem arises when the word is surrounded by single quotes. Not sure what's the problem. Any ideas?

Upvotes: 0

Views: 187

Answers (1)

MaxZoom
MaxZoom

Reputation: 7753

I would try to build the regex on the something-followedBy-notSomething-folowedBy-something approach. So it may look like below:

var input = "\"'I'm not.'\" - said Marry.";
var rex = /"([^"]+)"/g
alert(input.replace(rex, ''$1'')) 

DEMO

Upvotes: 1

Related Questions