JavaScript Regex that matches everything starting with a specific pattern until there's not a whitespace or newline

huangapple go评论61阅读模式
英文:

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://%$&amp;/()= 

and the start pattern it's

protocol:\/\/

has to gives back in matches:

&quot;protocol://EVERITIN&quot;
&quot;protocol://EVERITING2&quot;
&quot;protocol://%$&amp;/()=&quot;

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://%$&amp;/()= 
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 -->

huangapple
  • 本文由 发表于 2023年2月18日 01:39:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定