-1

what i need to do is look inside a string for something like "12345" and replace it with 12345 so basically search for any number within quotes and replace it with just the number without the quotes.

I've tried something but i don't know how to keep the evalueted number removing only the quotes.

Some examples:

foo bar "123" bar --> foo bar 123 bar

foo bar "123qwe" bar --> foo bar "123qwe" bar

foo "bar" "123" bar --> foo "bar" 123 bar

Thank you in advance.

Solved:

.replace(/"([0-9]+)"/g, '$1');

2 Answers2

0

You can do

function getResult(str){
    console.log(str.replace(/"(\d+)"/g, '$1'));
}

getResult('foo bar "123" bar');

getResult('foo bar "123qwe" bar');

getResult('foo "bar" "123" bar');
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

This regex should work for you "([0-9]+)" :

var s = 'test "test" 123 "123" ""';
s = s.replace(/"([0-9]+)"/g, "$1");
console.log(s);
Zenoo
  • 12,670
  • 4
  • 45
  • 69