简体   繁体   English

有没有办法用换行符(\\ n)符号搜索字符串中的子字符串?

[英]Is there a way to search for a sub-string inside a string with newline (\n) symbols?

I have a text with \\n characters inside and I have an array of phrases I want to highlight in this text by wrapping its segments with tag. 我有一个带有\\n字符的文本,我有一个短语数组,我希望通过用标记包装其段来突出显示。 The problem is that I can't find this phrases in text if there is a \\n symbol. 问题是如果有\\n符号,我在文本中找不到这个短语。

I've tried to replace \\n from text, but I need to restore them after highlight. 我试图用文本替换\\n ,但我需要在突出显示后恢复它们。

let text = 'Looking For An Enterprise Test Authoring Platform?\n
Learn More About Gauge\n
Watch our video to learn if Gauge can help you.'

let phrases = ["Authoring Platform? Learn More", "Gauge Watch our video", "can help you"]

const highlight = (phrase) => text.replace(phrase, `<mark style="background: #4CAF50">${phrase}</mark>`)

phrases.map(phrase=> text = highlight(phrase))

Only last phrase will match with text. 只有最后一个短语才会与文本匹配。 I'm looking for some way to ignore \\n and match all this phrases. 我正在寻找一些方法来忽略\\n并匹配所有这些短语。 Or maybe there is another way to solve this. 或许还有另一种解决方法。 I would appreciate any help! 我将不胜感激任何帮助!

One option is to loop through the phrase and create a dynamci regex. 一种选择是遍历短语并创建动态正则表达式。 Replace every \\s with (?:\\n)* . (?:\\n)*替换每个\\s This will create a dynamic regex like this: 这将创建一个像这样的动态正则表达式:

/Authoring(?:\n)* Platform\?(?:\n)* Learn(?:\n)* More/

Then replace the text with matched substring using $& . 然后使用$& replace匹配子字符串的text This will preserve the \\n from the original string. 这将保留原始字符串中的\\n

 let text = 'Looking For An Enterprise Test Authoring Platform?\\n Learn More About Gauge\\n Watch our video to learn if Gauge can help you.' let phrases = ["Authoring Platform? Learn More", "Gauge Watch our video", "can help you"] // https://stackoverflow.com/a/494122 const escape = str => str.replace(/([.?*+^$[\\]\\\\(){}|-])/g, "\\\\$1") phrases.forEach(p => { const regex = new RegExp( escape(p).replace(/\\s/g, '\\(?:\\n)* ') ) text = text.replace(regex, `<mark style="background:#4CAF50">$&</mark>`) }) console.log(text) 

The escape function is taken from here . escape函数取自此处 It is used to escape metacharacters like ? 它用于逃避元字符,如? from each phrase 从每个短语

Here's an alternative using reduce and some helper functions: 这是使用reduce和一些辅助函数的替代方法:

 const text = 'Looking For An Enterprise Test Authoring Platform?\\n Learn More About Gauge\\n Watch our video to learn if Gauge can help you.', phrases = ["Authoring Platform? Learn More", "Gauge Watch our video", "can help you"], escape = str => str.replace(/([.?*+^$[\\]\\\\(){}|-])/g, "\\\\$1"), createReg = p => new RegExp( escape(p).replace(/\\s/g, '\\(?:\\n)* ') ), replaceWith = '<mark style="background:#4CAF50">$&</mark>', output = phrases.reduce((a, p) => a.replace(createReg(p), replaceWith), text) console.log(output) 

You can potentially go around by taking the first word and matching your sentence with this simple regex \\bfirstWord (.*?) endWord\\b 你可以通过这个简单的正则表达式\\bfirstWord (.*?) endWord\\b来获取第一个单词并匹配你的句子\\bfirstWord (.*?) endWord\\b

let text = 'Looking For An Enterprise Test Authoring Platform?\n
            Learn More About Gauge\n
            Watch our video to learn ifGauge can help you.';

 text.match(/\Gauge (.*?) video\b/gis)
 // ["Gauge↵↵Watch our video"]
 // \n character is being preserved

It will probably make it a little more complicate though because you need to find the first and the last word of each sentence. 它可能会使它更复杂一点,因为你需要找到每个句子的第一个和最后一个单词。

Just remove the \\n from the string and check for phrases in that string like this. 只需从字符串中删除\\ n,然后像这样检查该字符串中的短语。

let text = 'Looking For An Enterprise Test Authoring Platform?\n
Learn More About Gauge\n
Watch our video to learn ifGauge can help you.';

let phrases = ["Authoring Platform? Learn More", "Gauge Watch our video", "can help you"];

//string without \n
let plainText = text.replace("\n", "");

const highlight = (phrase) => {
    return plainText.replace(phrase, `<mark style="background: #4CAF50">${phrase}</mark>`)
}


phrases.map(phrase=> {
    text = highlight(phrase)
})  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM