How to use ^/$ if it's already used as a delimiter for regex in PHP?

后端 未结 2 1798
猫巷女王i
猫巷女王i 2021-01-22 02:37
$regex = \'$....$\'

$regex = \'^...^\'

In the above two cases,how to use ^/$ to match the beginning/end of a string?

2条回答
  •  北海茫月
    2021-01-22 03:06

    You can use any non-alphanumeric, non-backslash, non-whitespace character as delimiter. But you shouldn’t use characters that have a special meaning in regular expressions.

    So you could use the ~ if you don’t want to use /:

    '~^/$~'
    

提交回复
热议问题