有人知道我该怎么做吗?我需要找到下面的单词直到结束点。
文本示例:
产品: 我的好产品。
我需要:我的好产品
我已经设法找到下一个单词,但没有找到其他单词,直到最后一点使用此代码:
const product= 'PRODUCT:';
let result = text.match(new RegExp(product+ '\\s(\\w+)', 'i'));
if (result != null) {
result = result[1];
}
谢谢。
最佳答案
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>// Input = Product: My wonderful product.
// Output = My wonderful product
const text = 'Product: My wonderful product.';
const product = 'PRODUCT: ';
const matches = text.match(new RegExp(`(?:${product})(.*)`, 'i'));
let result;
if (matches && matches.length === 2) {
result = matches[1];
} else {
result = '';
}
console.log(result)</code></pre>
</div>
</div>
见 https://regex101.com/r/ZA4qHz/1/进行调试。
相关文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
关于javascript - 使用正则表达式 javascript 查找要终结的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54994588/