How to escape slashes in regex?

前端 未结 2 1845
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 20:23

I\'ve made some regex to test for a YouTube embedded video:

/^(http:\\/\\/www\\.youtube\\.com\\/embed\\/)[^\\/\\s\\\\]+$/

It works for what I e

相关标签:
2条回答
  • 2021-01-21 21:03

    In PHP, you can also use a different regex delimiter:

    ~^(http://www\.youtube\.com/embed/)[^/\s\\\\]+$~
    
    0 讨论(0)
  • 2021-01-21 21:11

    If you have a string, you will need to escape the backslashes (and quotes) for the string literal. Or, depending on how the function builds the regex from the string, you might not need to escape slashes at all (I don't think so here).

    "iframe=-*,src(match=\"/^(http:\\/\\/www\\.youtube\\.com\\/embed\\/)[^\\/\\s\\\\]+$/\")"
    
    0 讨论(0)
提交回复
热议问题