简体   繁体   中英

Regex to replace entire match

I have this regex in JavaScript:

var val = val.replace(/[":)"]/g, "<img src = \"/img/smile.png\" height = 24 width = 24>");

Basically, this regex should replace :) with an image of a smiley, however it is placing an image of a smiley face on either : or ) not when both are next to each other.

How must I adapt this regex: /[":)"]/g to only replace if there is an exact match for :) not just one on it's own?

Thanks

What you have now is a character class, meaning it's looking to match either a " , : , or ) and replace it with the image.

To fix this, your regex should be /:\\)/g :

var val = val.replace(/:\)/g, "<img src = \"/img/smile.png\" height = 24 width = 24>");

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