英文:
JavaScript Regex that matches everything starting with a specific pattern until there's not a whitespace or newline
问题
Sure, here is the translated content you requested:
我在寻找一个适用于JavaScript 2015/ES5及更高版本的正则表达式,该正则表达式匹配从某个模式开始直到没有空格或换行符,但不包括最后一个空格或换行符。
我不是在寻找一个单一的模式,但它必须可以替换为你创建的内容。
假设初始文本是:
protocol://EVERITING protocol://EVERITING2
protocol://%$&/()=
而起始模式是:
protocol:\/\/
那么匹配结果应该是:
"protocol://EVERITIN"
"protocol://EVERITING2"
"protocol://%$&/()="
是否可以实现这样的匹配?
英文:
I was searching for a <b>JavaScript</b> 2015/ES5 and up regex that matches everything starting with a pattern until there's not a blank space or newline, but that does not take in the last space or newline
I'm not looking for a single pattern, but it has to be exchangeable for what you made.
Assuming that the starting text it's
protocol://EVERITING protocol://EVERITING2
protocol://%$&/()=
and the start pattern it's
protocol:\/\/
has to gives back in matches:
"protocol://EVERITIN"
"protocol://EVERITING2"
"protocol://%$&/()="
It's possible to make something like this?
答案1
得分: 0
In javascript (2015/ES5 and up), this should yield the result you're looking for:
const input = `protocol://EVERITING protocol://EVERITING2
protocol://%$&/()=
protocol://sdofihsofhia;sjf.ew/asjodfihapc80hem/ test
donotmatch
protocol:/s/alsodontmatch`
const matches = input.matchAll(/protocol:\/\/\S+/g)
for (const match of matches) {
console.log(`Found ${match[0]} start=${match.index} end=${match.index + match[0].length}`);
}
// alternatively, in more condensed format:
console.log([...input.matchAll(/protocol:\/\/\S+/g)])
英文:
In javascript (2015/ES5 and up), this should yield the result you're looking for:
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const input = `protocol://EVERITING protocol://EVERITING2
protocol://%$&/()=
protocol://sdofihsofhia;sjf.ew/asjodfihapc80hem/ test
donotmatch
protocol:/s/alsodontmatch`
const matches = input.matchAll(/protocol:\/\/\S+/g)
for (const match of matches) {
console.log(`Found ${match[0]} start=${match.index} end=${match.index + match[0].length}`);
}
// alternatively, in more condensed format:
console.log([...input.matchAll(/protocol:\/\/\S+/g)])
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论