gpt4 book ai didi

javascript regex .match 在某个字符串处停止

转载 作者:行者123 更新时间:2023-11-29 14:56:34 25 4
gpt4 key购买 nike

我应该在这个正则表达式中添加什么来停止在“/stopHere”处的搜索,并且只返回它在该字符串之前找到的内容?

var string = "http://website.com/#!/goodText=hello/hello//stopHere=badtext";

var reg = string.match(/goodText=.*\/stopHere/);

这捕获 ["goodText=hello/hello/hello/stopHere"] ,那么我将如何使它排除“/stopHere”?

谢谢!

最佳答案

不知道这是否是最好的方法,但找到了一个叫做 Positive Lookahead 的东西:

var string = "http://website.com/#!/goodText=hello/hello//stopHere=badtext";    
var reg = string.match(/goodText=.*(?=\/stopHere)/);
// returns: goodText=hello/hello/

基本上如果我把 (?=regex)最后,当它发现时它会停止!

关于javascript regex .match 在某个字符串处停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16887636/

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