简体   繁体   English

Javascript中的字符串通配符替换或删除

[英]String wildcard replace or delete in Javascript

I would like to replace or delete substring inside a sentence using Js or reactJS.我想使用 Js 或 reactJS 替换或删除句子中的子字符串。

The below is my sample sentence.下面是我的例句。

RAND Corporation is an American nonprofit global policy think tank created in 1948 by Douglas Aircraft Company to offer research and analysis to the United States Armed Forces. 
It is financed by the U.S. government and private endowment, corporations, universities and private individuals

Source: <Origin Href="Link">https://rand.org</a>
Thanks ..... 

In this sentence, I need to find Source: <Origin Href="Link">https://rand.org</a>在这句话中,我需要找到Source: <Origin Href="Link">https://rand.org</a>

So basically it will be something like Source * </a> Source & </a> will be the common string in other sentences too.所以基本上它会像Source * </a> Source & </a>将是其他句子中的常见字符串。 * is the wildcard, may contain various other texts. *是通配符,可能包含各种其他文本。

So, how to remove this line or replace with space or something.那么,如何删除此行或替换为空格或其他内容。

My current code is我目前的代码是

var sb = "RAND Corporation is an American nonprofit global policy think tank created in 1948 by Douglas Aircraft Company to offer research and analysis to the United States Armed Forces. It is financed by the U.S. government and private endowment, corporations, universities and private individuals Source: <Origin Href=\"Link\">https:\/\/rand.org</a> Thanks ..... "

var result = sb.replace(/\nSource:.*<\/a>/, '')

alert(result)

Remove the entire line?删除整行? just use a regex: /\\nSource:.*<\\/a>/ ( . matches any non-linebreak char, * means repeat 0 or more times, the / in </a> needs to be escaped)只需使用正则表达式: /\\nSource:.*<\\/a>/.匹配任何非换行符, *表示重复 0 次或多次, </a>/需要转义)

yourString.replace(/\nSource:.*<\/a>/, '')

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

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