gpt4 book ai didi

Javascript 正则表达式积极前瞻

转载 作者:行者123 更新时间:2023-11-30 07:09:49 25 4
gpt4 key购买 nike

将正则表达式 word 的开头和结尾定义为捕获组中的边界的正确语法是什么,如下所示:

var $classes = $( 'body' ).attr('class'); // string 'foo bar sub-theme-home';
var regex = /sub-theme-(?=(\b))/;
var sub_theme = regex.exec($classes);
console.log(sub_theme[0]);

这是匹配 sub-theme-,虽然如果我需要将它包含在我的捕获中并进行字符串替换以将其删除是没问题的,但我无法找出适合我的语法的正确语法(?-(\b))/ 捕获组。看起来像积极的前瞻,sub-theme- 应该是省略的部分,home 应该被捕获...帮助?

最佳答案

为什么不直接捕获您要查找的字符串?

var $classes = $( 'body' ).attr('class'); // string 'foo bar sub-theme-home';
var regex = /sub-theme-(\w+)/;
var sub_theme = regex.exec($classes);
console.log(sub_theme[1]);

MDN 查看更多关于带括号的子字符串和 regex.exec(...) 的结果的信息.

关于Javascript 正则表达式积极前瞻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13150800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com