将正则表达式转换为PHP
translate regex to PHP
我很难将这个函数移植到PHP。
SomeString.replace(/([.*+?^=!:${}()|'[']'/''])/g, "''$1");
老实说,我甚至不知道它到底是干什么的。我尝试至少将表达式与preg_replace
一起使用但得到
preg_match():编译失败:在偏移量25 处缺少字符类的终止]
当使用类似的东西时
preg_match('/([.*+?^=!:${}()|''['']''/''])/', $string, $matches);
javascript函数.replace
由preg_replace
在php中翻译,因此:
SomeString.replace(/([.*+?^=!:${}()|'[']'/''])/g, "''$1");
变为:
$SomeString = preg_replace('~([.*+?^=!:${}()|'[']/''''])~', "''''$1", $SomeString);
这将单独替换字符类中已转义的特殊字符。
除此之外,您遇到的错误是由于角色在尝试使用preg_match时使用了双转义符,因此必须使用双转义符。
preg_match('/([.*+?^=!:${}()|'[']'/''''])/', $string, $matches);
// | ^^^^^ double-double escape the backslash
// ^ no needs to double escape here
您可以使用:
preg_match('#([.*+?^=!:${}()|'[']/''''])#', $string, $matches);
您的错误是在正则表达式中使用了''
而不是''''
。匹配反斜杠需要双转义。一个用于PHP的''
和另一个用于PCRE引擎的''
。
相关文章:
- 使用Javascript将PHP转换为HTML5
- 如何将JSON从PHP转换为JS
- 将PHP转换为JavaScript,自调用函数
- 从sqlite PHP转换数据
- 注意:将数组从 php 转换为 javascript 时出现数组到字符串的转换错误
- 使用 PHP 转换 SQL 时间
- 如何将其从PHP转换为JavaScript
- 将数组从php转换为javascript
- php转换为javascript并返回
- 将jsonencode php转换为javascript,但不要使用双引号
- 将PHP转换为javascript #1
- 将逗号php转换为javascript
- 用PHP转换知名文本(WKT)从MySQL到谷歌地图多边形
- 将javascript数组从PHP转换为javascript函数
- 通过PHP转换XML &Javascript函数
- 将数学方程式从PHP转换为Javascript (Jquery)
- 使用php转换为json
- 如何将格式日期从PHP转换为Javascript
- 如何将这个PHP转换为express.js/node
- 用于转换从 PHP 转换的序列号的 JavaScript 代码