[英]Add prefix and suffix to all occurrences of a word in a string in JavaScript
I have a text file which I can read as a string for example something like this...我有一个文本文件,我可以将其作为字符串读取,例如这样的东西......
Hello This is a test string this is a test THIS is just a test ok?
您好 这是一个测试字符串 这是一个测试 这只是一个测试 可以吗? Can we solve This?
我们能解决这个问题吗? Idk, maybe thiS, is just impossible.
Idk,也许这,是不可能的。
I want the output to append "Foo" to the front and "Bar" to the back of every "this" word (case insensitive) so that the output will look like this:我希望输出将“Foo”附加到每个“this”单词的前面,并将“Bar”附加到每个“this”单词的后面(不区分大小写),以便输出如下所示:
Hello FooThisBar is a test string FoothisBar is a test FooTHISBar is just a test ok?
你好 FooThisBar 是一个测试字符串 FoothisBar 是一个测试 FooTHISBar 只是一个测试好吗? Can we solve FooThisBar?
我们能解决 FooThisBar 吗? Idk, maybe FoothiSBar, is just impossible.
Idk,也许是 FoothiSBar,是不可能的。
"this"
"this"
每个出现Simple replace
with a capturing group :用捕获组简单
replace
:
const str = "Hello This is a test string this is a test THIS is just a test ok? Can we solve This? Idk, maybe thiS, is just impossible."; const result = str.replace(/(this)/gi, "Foo$1Bar") console.log(result)
"this"
when it is a word (works with punctuation)"this"
(使用标点符号) To avoid matching "this"
inside a word (eg, "abcthisdef"
), you can use a negative lookahead and negative lookbehind :为避免在单词内匹配
"this"
(例如, "abcthisdef"
),您可以使用否定前瞻和否定后视:
const str = "Hello This is a test string this is a test THIS is just a test ok? Can we solve This? Idk, maybe thiS, is just impossible."; const result = str.replace(/(?<!\\w)(this)(?!\\w)/gi, "Foo$1Bar") console.log(result)
You can split the string by a space, map through the resulting array and return the modified string only when the item (when converted to lowercase) is equal to "this"
:您可以用空格分割字符串,映射到结果数组并仅当项目(转换为小写时)等于
"this"
时才返回修改后的字符串:
const str = "Hello This is a test string this is a test THIS is just a test ok? Can we solve This? Idk, maybe thiS, is just impossible."; const result = str.split(" ").map(e => e.toLowerCase() == "this" ? `Foo${e}Bar` : e).join(' ') console.log(result)
The caveat with the above solution is that it will not match "this"
when it is beside punctuation.上述解决方案的警告是,当它在标点符号旁边时,它不会匹配
"this"
。 Eg, it will not match "this."
例如,它不会匹配
"this."
. .
To also replace words with trailing punctuation, you can first split the string with a regex that matches non-alphanumeric words, check whether the first item is "this"
, then concatenate the second item after (after first join
ing, since the second item in the destructure assignment is an array of trailing punctuation characters):要也用尾随标点符号替换单词,您可以首先使用匹配非字母数字单词的正则表达式拆分字符串,检查第一项是否为
"this"
,然后在之后连接第二项(在第一次join
之后,因为第二项在解构赋值中是一个尾随标点字符数组):
const str = "Hello This is a test string this is a test THIS is just a test ok? Can we solve This? Idk, maybe thiS, is just impossible. this??? this!"; const result = str.split(" ").map(e => { let [word, ...punctuation] = e.split(/(?!\\w)/g) return word.toLowerCase() == "this" ? `Foo${word}Bar${punctuation.join('')}` : e }).join(' ') console.log(result)
Note that this solution will not work if there is punctuation before the occurence.请注意,如果出现之前有标点符号,则此解决方案将不起作用。 For example, it will convert
"this"
to this"
. To avoid this, use the recommended regex solution above.例如,它会将
"this"
转换为this"
。为避免这种情况,请使用上面推荐的正则表达式解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.