0

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)
Muneeb K
  • 457
  • 1
  • 9
  • 21

1 Answers1

1

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>/, '')
  • Am not getting the answer ... am adding the code in my question – Muneeb K Oct 12 '20 at 17:21
  • @MuneebK you stripped the lineBreak chars, my regex tries to match for one at the beginning to fully remove the line instead of just leaving it empty. try to read up a bit on RegExp in JavaScript, if you don't know what i mean – Kilian Kilmister Oct 12 '20 at 17:50
  • Yes , I got it. This will work for the new line when source appears. How about anywere in the sentence – Muneeb K Oct 12 '20 at 19:00