开发者

Combining regex in jQuery

开发者 https://www.devze.com 2023-02-10 22:54 出处:网络
I have this jQuery code which replaces some HTML character codes with their equivalent: $(\'.someText\').html()

I have this jQuery code which replaces some HTML character codes with their equivalent:

$('.someText').html()
              .replace(/&/g, '&')
              .replace(/&lt;/g, '<')
              .replace(/&gt;/g, '>');

That works and all, but is there a way to combine those replace oper开发者_开发知识库ations all into one regex to make the code more concise?

I'm still wrapping my head around regular expressions


This question has a solution that is more flexible, in case you ever expand beyond just the basic three entities:

Converting sanitised html back to displayable html


I answered a similar question a while ago. Simple answer; there may be a way to do it, but it's probably better to do what you have here. Each replacement is it's own operation, so might as well leave it that way.

0

精彩评论

暂无评论...
验证码 换一张
取 消