简体   繁体   中英

String wildcard replace or delete in Javascript

I would like to replace or delete substring inside a sentence using Js or 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>

So basically it will be something like Source * </a> Source & </a> will be the common string in other sentences too. * 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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