2

i want to use Regex in div at HTML in order to replace a certain string, this string is like : age:7Refat"student" or it will be like age:7Refat , i'm using the following command that is ok with the second pattern:

$("#order_list").append($(this).text().replace(new RegExp("age:[0-9]+","g"),''));

but what if i want to use a general command for both patterns, the something is i don't know how to deal with the first pattern as it has double quotes"" , and i can't write:

$("#order_list").append($(this).text().replace(new RegExp("age:[0-9]+"[a-z]"","g"),''));

or

$("#order_list").append($(this).text().replace(new RegExp("price:[0-9]+[\"a-z\"]","g"),''));
Refaat
  • 107
  • 1
  • 4
  • 13
  • 1
    This is first and foremost a basic javascript error. You need to escape the double quotes: "**\"**". – JDB Sep 18 '13 at 20:20
  • don't work, gives me a syntax error – Refaat Sep 18 '13 at 20:25
  • You can also check the answer from [here](https://stackoverflow.com/questions/9343082/html5-input-pattern-search-for-quote) – Mobee Mar 15 '22 at 06:45

3 Answers3

1

Either escape the quotes like you did in your third example (but I think you put them in the wrong place):

new RegExp("price:[0-9]+\"[a-z]\"","g")

or (better) use a regex literal:

/price:[0-9]+"[a-z]"/g
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • this don't work, it gives me a syntax error as it reads the first quote as it the end of the exp – Refaat Sep 18 '13 at 20:25
  • Have you wrapped the regex literal inside of quotes? Don't: the `/` character delimit the regex (for example: `/price:[0-9]+"[a-z]"/g` *not* `"/price:[0-9]+"[a-z]"/g"`, which *is* a syntax error). – David Thomas Sep 18 '13 at 20:38
  • i used the regexp you wrote ->>> new RegExp("price:[0-9]+\"[a-z]\"","g") – Refaat Sep 18 '13 at 20:48
  • 1
    @Refaat - Sounds like you have an unmatched quote further upstream. – JDB Sep 18 '13 at 21:09
0

You may try this

$("#order_list").append($(this).text().replace(new RegExp("age:[0-9]+\"[a-z]\"","g"));

instead of this:-

$("#order_list").append($(this).text().replace(new RegExp("age:[0-9]+","g"),''));
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • this don't work, it gives me a syntax error as it reads the first quote as it the end of the exp – Refaat Sep 18 '13 at 20:23
0

That finally works with me :

$("#order_list").append(($(this).text().replace(new RegExp("age:[0-9]+","g"),'')).replace(new RegExp("[a-zA-Z]+","g"),'').replace(new RegExp("\"+","g"),''));
Refaat
  • 107
  • 1
  • 4
  • 13