简体   繁体   中英

Replace words in a paragraph using Javascript

I have a paragraph of some texts. I want to replace some words in that using wildcard.

The below is my Paragraph.

      0.7% lower on the prospect of fresh restrictions that would deal a blow to hopes of a swift economic
     recovery.  <Origin Href=\"StoryRef\">urn:newsml:reuters.com:*:nL4N2EC04Z</Origin>\n The 2,000-plus cases reported on Sunday was a shocker ,said Nicholas Mapa, ING

In this Para, I want to remove <Origin Href=\\"StoryRef\\">urn:newsml:reuters.com:*:nL4N2EC04Z</Origin>\\n There are multiple paragraphs. But only the uncommon one is nL4N2EC04Z
All other words are common in those paragraphs .

<Origin Href=\"StoryRef\">urn:newsml:reuters.com:*:(need_to_use_wild_card_here)</Origin>\n

I tried to replace one half. My code

storyRef="<Origin Href=\"StoryRef\">urn:newsml:reuters.com:*:";
storyRef.replace(storyRef," ")

But am stuck in replacing other parts.

It seems encoding problem. Try to use JSON.stringify to ensure that characters like < and some others does not read decoded.

You can improve Regex too to something like this 👇

storyRef.replace(/<Origin.*Origin>\\n/gm, ' ');

This example will start in <Origin , get all content between until Origin>\\n .

 const p = JSON.stringify('0.7% lower on the prospect of fresh restrictions that would deal a blow to hopes of a swift economic recovery. <Origin Href=\\"StoryRef\\">urn:newsml:reuters.com:*:nL4N2EC04Z</Origin>\\n The 2,000-plus cases reported on Sunday was a shocker ,said Nicholas Mapa, ING'); const storyRef = JSON.parse(p.replace(/(<Origin.*Origin>\\\\n)+/gm, ' ')); console.log(storyRef);

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