英文:
Please help me with regular expression
问题
正则表达式不起作用。
我需要标记字符串的一部分,但我不知道应该怎么做。
const markPartOfString = (string, substrings) => {
if (!substrings) return string;
const parts = substrings.split(',').map(str => str.trim());
let markedString = string;
parts.forEach(part => {
const regex = new RegExp(`\\b${part}\\b`, 'gi');
markedString = markedString.replace(regex, match => `<mark>${match}</mark>`);
});
return markedString;
}
// 期望的结果
console.log(markPartOfString("wfo test3", "wfo test, wfo test3, wf te")); // <mark>wfo</mark><mark>test3</mark>
console.log(markPartOfString("wfo test", "wf")); // <mark>wf</mark>0 test
console.log(markPartOfString("wfo test", "wf te")); // <mark>wf</mark>o <mark>te</mark>st
console.log(markPartOfString("wfo test", "wfo")); // <mark>wfo</mark> test
console.log(markPartOfString("wfo test", "o st")); // wf<mark>o</mark> <mark>st</mark>est
英文:
Regular expression not working correctly.
I need to mark part of string, but I don't know how it is do
const markPartOfString = (string, substrings) => {
if (!substrings) return string;
const parts = substrings.split(',').map(str => str.trim());
let markedString = string;
parts.forEach(part => {
const regex = new RegExp(`\\b${part}\\b`, 'gi');
markedString = markedString.replace(regex, match => `<mark>${match}</mark>`);
});
return markedString;
}
//Expected result
console.log(markPartOfString("wfo test3", "wfo test, wfo test3, wf te")); // <mark>wfo</mark><mark>test3</mark>
console.log(markPartOfString("wfo test", "wf")); // <mark>wf</mark>0 test
console.log(markPartOfString("wfo test", "wf te")); // <mark>wf</mark>o <mark>te</mark>st
console.log(markPartOfString("wfo test", "wfo")); // <mark>wfo</mark> test
console.log(markPartOfString("wfo test", "o st")); // wf<mark>o</mark> <mark>st</mark>est
答案1
得分: 1
我认为以下示例有效,除了第一个情况,它不会产生相同的结果,因为在输入中存在逗号时,不清楚期望的行为是什么。
另外:
- 不要忘记转义在正则表达式中使用的动态值
- 不要忘记转义在HTML中使用的动态值
希望的结果:
console.log(markPartOfString("wfo test3", "wfo test, wfo test3, wf te")); // <mark>wfo</mark><mark>test3</mark>
console.log(markPartOfString("wfo test", "wf")); // <mark>wf</mark>0 test
console.log(markPartOfString("wfo test", "wf te")); // <mark>wf</mark>o <mark>te</mark>st
console.log(markPartOfString("wfo test", "wfo")); // <mark>wfo</mark> test
console.log(markPartOfString("wfo test", "o st")); // wf<mark>o</mark> <mark>st</mark>est
英文:
I think the below example works, except the first case which doesn't produce the same result, because it's not clear what the desired behaviour is when there are commas in the input.
Also:
- Don't forget to escape the dynamic values used in a RegExp
- Don't forget to escape the dynamic values used in HTML
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\$&');
}
// https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript
function escapeHtml(str) {
return str
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
const markPartOfString = (string, substrings) => {
if (!substrings) return string;
const parts = substrings.split(',')
.flatMap(str => str.split(' '))
.map(str => escapeRegExp(str.trim()))
.filter(str => !!str);
const regex = new RegExp(`(${parts.join('|')})`, 'gi');
return string.replace(regex, match => `<mark>${escapeHtml(match)}</mark>`);
}
//Expected result
console.log(markPartOfString("wfo test3", "wfo test, wfo test3, wf te")); // <mark>wfo</mark><mark>test3</mark>
console.log(markPartOfString("wfo test", "wf")); // <mark>wf</mark>0 test
console.log(markPartOfString("wfo test", "wf te")); // <mark>wf</mark>o <mark>te</mark>st
console.log(markPartOfString("wfo test", "wfo")); // <mark>wfo</mark> test
console.log(markPartOfString("wfo test", "o st")); // wf<mark>o</mark> <mark>st</mark>est
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论