0

I have an API that returns an HTML string but it has some extra characters in the API and I want to replace that with something else.

<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0\">

I want to replace \" with " using that using replace so that it becomes

<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0">

What is the regex string.replace() for doing the above task?

1
  • What is the source of your input string on line 2 above? Those backslashes look to be escaping characters for the literal double quotes which follow them. That is, the backslashes aren't really "there." Commented Apr 13, 2021 at 4:38

3 Answers 3

1

use the \\ into your regexp

var text = '<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0\">';

var result  = text.replace(/\\"/g, '"');

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

So you have to escape the \ with another \

yourstring.replace(/\\"/, '"')

Comments

1

You can simply use this without RegEx

<?php
$a = "<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0\">";
$b = str_replace('\"', '"', $a);
var_dump($b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.