Javascript js regex从txt获取字符串,以“开始”;“开始”;以“结束”;完";只有一次
你好, 我有文本,我需要在其中找到一个字符串,以“start”开头,以“end”结尾。只有第一次发生。 如果字符串为:Javascript js regex从txt获取字符串,以“开始”;“开始”;以“结束”;完";只有一次,javascript,regex,Javascript,Regex,你好, 我有文本,我需要在其中找到一个字符串,以“start”开头,以“end”结尾。只有第一次发生。 如果字符串为: "just test ... bla ...bla start some txt .... end more text " "just test ... bla ...bla start some txt .... end more text ".match(/start(.*)end/) 我得到了正确的答案,包含2项: array[0]="start some txt
"just test ... bla ...bla start some txt .... end more text "
"just test ... bla ...bla start some txt .... end more text ".match(/start(.*)end/)
我得到了正确的答案,包含2项:
array[0]="start some txt .... end"
array[1]=" some txt .... "
这太完美了。
当文本包含多个“结束”时:
我无法获得正确的字符串发生的情况是,.match(/start(.*)end/)
返回从第一个“开始”到最后一个“结束”的所有字符串,而不是返回到第一个“结束”:
返回:“开始一些文本…结束更多文本xxxx结束蓝色fff…结束”
我需要的只是第一次出现:
"start some txt .... end"
谢谢你的帮助
/start(.*?)end/
/start(.*?)end/
?
将其设置为“非贪婪”,这意味着它将尽可能少地匹配尝试非贪婪匹配:
/start(.*?)end/