-2

I want to replace <span class="test">,</span> to , I have tried as

if($('.elq-form').hasClass('elq-form')) {
   $(".elq-form").html( $(".elq-form").html().replace(/<span class="test">,</span>/g,",") ); }
} 

I'm getting

Uncaught SyntaxError: Invalid regular expression flags

how to replace the span tag with comma(,) ?

Pedram
  • 15,766
  • 10
  • 44
  • 73
user3386779
  • 6,883
  • 20
  • 66
  • 134
  • 2
    You shouldn't use regex for html: https://stackoverflow.com/a/1732454/8796313 – Joost K Jan 06 '20 at 13:08
  • what is this `if($('.elq-form').hasClass('elq-form')) {` :| if you select element by `.elq-form` class, so it has this class ~! – Pedram Jan 06 '20 at 13:33

2 Answers2

5

Simply use unwrap

$(".elq-form").find('span.test').contents().unwrap();
.test {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="elq-form">
  <span class="test">,</span>
  <a>Blah Blah</a>
  <span class="test">,</span>
  <p>Blah Blah</p>
</div>
Pedram
  • 15,766
  • 10
  • 44
  • 73
  • 2
    You may want `.find("span.test")` since the OP's code was specific to spans. (Who knows, maybe they have other `.test` elements...) – T.J. Crowder Jan 06 '20 at 13:30
1

/ has special meaning in RegEx, you have to escape (\/) that:

$(".elq-form").html( $(".elq-form").html().replace(/<span class="test">,<\/span>/g,",") );
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • 1
    True, but it's still a Bad Idea.™ :-) *(And surely there's a dupetarget, if this is the answer to the question...)* – T.J. Crowder Jan 06 '20 at 13:11