1

I would like to replace the simple quotes in a string but not the escaped ones (in JavaScript). For example I want this string:

'Hello there! I\'m \'Doug\''

To become:

"Hello there! I\'m \'Doug\'"

NOTE: I made a lots of research and test (e.g on regex101.com) and found some things such as Regular expression to match a line that doesn't contain a word? but still can't figure out how to build a proper regex.

THINGS I TRIED:

  • /[^\\]'/g
  • /\\{0}'/g
  • /[\\]{0}'/g
  • ...
Community
  • 1
  • 1
Gabin
  • 920
  • 10
  • 26

4 Answers4

1

You can use this String#replace with callback function:

str = "'Hello there! I\\'m \\'Doug\\''";
str = str.replace(/(\\)?'/g, function($0, $1) {
   return ($1) ? "\\'" : '"';
});
console.log( str );
//=> "Hello there! I\'m \'Doug\'"
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • A bit weird and my strings doesn't contain a double \ to escape a simple quote. – Gabin Oct 03 '14 at 19:01
  • It is not double quote if you print it, it will show only single \ – anubhava Oct 03 '14 at 19:04
  • Yes sure but your original string contains a double backslash, not mine. My example is `'Hello there! I\'m \'Doug\''` not `'Hello there! I\\'m \\'Doug\\''` – Gabin Oct 03 '14 at 19:08
  • You're mistaken. Use `alert("'Hello there! I\'m \'Doug\''")` to see what gets printed. – anubhava Oct 03 '14 at 19:10
  • This is what I am trying to tell you. [See this jsfiddle](http://jsfiddle.net/mbwy757f/1/) and see what first `console.log(str)` prints i.e. no / in the output. – anubhava Oct 03 '14 at 19:20
  • The console does not print it but it still exist in the string. For example: http://jsfiddle.net/mbwy757f/2/ – Gabin Oct 03 '14 at 19:28
  • That is **exactly** what I have been saying. Creating string in Javascript will need double / but getting it from HTML will be fine with single /. **[See my working code here](http://jsfiddle.net/mbwy757f/3/)** which prints **`"I\'m doug!"`** – anubhava Oct 03 '14 at 19:31
  • 1
    My bad, you was right! It's actually a bit strange but now I get it :) Thank you very much for your time. – Gabin Oct 03 '14 at 20:11
0

This is close:

subject.replace(/^'|([^\\])'/g, '$1"')

It won't catch two apostrophes in a row, but I suppose you could just run it twice.

Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
  • Thank you but what if the string contains X apostrophes? How much times will I have to run it? Seems like there is probably a best way to do this. – Gabin Oct 03 '14 at 18:40
  • If it contains X apostrophes, you'll have to run it twice. It will replace every other one. – Jeremy Stein Oct 03 '14 at 21:39
0

Unfortunately, javascript besides all its portability and power doesn't support lookbehinds.

I made a regex to work well in the case of having multiple apostrophes too:

^'|[^\\](')[']+|([^\\])'

Replace with $1"

Online demo

revo
  • 47,783
  • 14
  • 74
  • 117
  • You're getting closer but `"Hello there! I\''''''m \'Doug\'"` should result in `"Hello there! I\'"""""m \'Doug\'"` – Gabin Oct 03 '14 at 18:59
-1

Maybe you need just this:

var text = "'Hello there! I'm \'Doug\''"; 
text.replace(/^\'|'$/g, '"')
speedy32
  • 61
  • 1
  • 3