RegEx to Reject Unescaped HTML Character

前端 未结 2 1394
野趣味
野趣味 2021-01-24 14:02

I want to restrict usage of unescaped ampersands in a particular input field. I\'m having trouble getting a RegEx to kill usage of \"&\" unless followed by \"amp;\"...or per

2条回答
  •  面向向阳花
    2021-01-24 14:43

    This regular expression matches any occurrence of & which is not followed by amp;:

    /&(?!amp;)/
    

    Rubular

    This regular expression accepts strings that contain characters except &, or the string &:

    /^([^&]|&)*$/
    

    Rubular

    You can use either one or the other, depending on which is most convenient. The difference is that the string should be rejected if the first regular expression matches, whereas the string should be accepted if the second regular expression matches.

提交回复
热议问题