Javascript RegEx quote

前端 未结 3 1849
感情败类
感情败类 2020-12-21 01:28

I\'m having trouble getting this Javascript Regular Expression to work. What i want is to find a character that starts with @\" has any characters in the middle

3条回答
  •  感情败类
    2020-12-21 01:43

    Try this regular expression:

    /@(["'])[^]*?\1/g
    

    An explanation:

    • @(["']) matches either @" or @'
    • [^]*? matches any arbitrary character ([^] contains all characters in opposite to . that doesn’t contain line-breaks), but in a non-greedy manner
    • \1 matches the same character as matches with (["'])

    Using the literal RegExp syntax /…/ is more convenient. Note that this doesn’t escape sequences like \" into account.

提交回复
热议问题