从 HTML 元素内的在线 javascript 函数调用内部获取号码
我试图匹配 open('')
内的整数,但收到错误:
警告:preg_match():找不到结束分隔符“^”
这是我的代码:
$linkvar ="<a onclick=\"javascript:open('597967');\" class=\"links\">more</a>";
preg_match("^[0-9]$", $linkvar, $result);
I'm trying to match the whole number inside of open('')
, but I am getting the error :
Warning: preg_match(): No ending delimiter '^' found
Here is my code:
$linkvar ="<a onclick=\"javascript:open('597967');\" class=\"links\">more</a>";
preg_match("^[0-9]quot;, $linkvar, $result);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
^
和$
修饰符,您的正则表达式仅在整个字符串由一个数字组成的情况下匹配。您当前的正则表达式用人类语言翻译为:^
表示“这是字符串的开头”[0-9]
表示“匹配单个数字字符”$
表示“这是字符串的结尾”将其更改为:
或者,匹配数字的简写语法:
+
修饰符表示必须找到“一个或多个”数字为了让它成为一场比赛。此外,如果您想实际捕获字符串内的数字,则需要添加括号来通知
preg_match
您实际上想要“保存”数字。Your regex only matches if the entire string is made up of one number because of the
^
and$
modifiers. Your current regex translates in human language to:^
means "this is the start of the string"[0-9]
means "match a single numeric character"$
means "this is the end of the string"Change it to:
Or alternatively, the shorthand syntax for matching numbers:
The
+
modifier means that "one or more" numbers must be found in order for it to be a match.Additionally, if you want to actually capture the numbers inside the string you'll need to add parentheses to inform
preg_match
that you actually want to "save" the numbers.仅当字符串恰好是一位数字时,您的正则表达式才会匹配。要仅匹配引号内的数字,请使用:
Your regex will only match if the string is exactly one digit. To match only the digits inside the quotes, use:
^ 和 $ 匹配字符串的开头和结尾,这意味着您当前正在搜索仅包含单个数字的字符串。删除它们并添加一个加量词,只留下“[0-9]+”,它将找到字符串中的第一组数字。
The ^ and $ match the start and end of the string, which means you are currently searching for a string containing ONLY a single digit. Remove them and add a plus quantifier, leaving just "[0-9]+", and it will find the first group of digits in the string.