简体   繁体   中英

jQuery replace span contains comma with single comma

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(,) ?

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>

/在 RegEx 中具有特殊含义,您必须转义 ( \\/ ):

$(".elq-form").html( $(".elq-form").html().replace(/<span class="test">,<\/span>/g,",") );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM