0

I have a multi string:

My input:

var str = `The left lower lung thick parenchymal band as seen on prior 
           scan remains not hypermetabolic.The posterior left lung base tiny
           density as seen on prior scan remains not significantly 
           hypermetabolic.`;
var element = 'The left lower lung thick parenchymal band as seen on prior scan remains not hypermetabolic';
var newElement = '<p1>The left lower lung thick parenchymal band as seen on prior scan remains not hypermetabolic</p1>';

How can I replace element with newElement in my original str using a regex?

My attempt:

str.replace(/[^\\S ]/, ' ').replace(element, newElement) 

When I try to replace using regex, I am not able to ignore the newlines and replace them at the same time.

Expected Result:

var str = `<p1>The left lower lung thick parenchymal band as seen on prior
            scan remains not hypermetabolic</p1>.The posterior left lung base tiny  
            density as seen on prior scan remains not significantly
            hypermetabolic.`;
Vikram
  • 79
  • 1
  • 11
  • How do you select upto what it should go to search1 and search2 ? – Code Maniac Feb 10 '20 at 12:00
  • I'm afraid it's really unclear what you're asking. Can you edit the question to say what your starting point is, what end result you want, and where you're stuck? With the above it's not clear to me what `search1` and `search2` are, for instance. – T.J. Crowder Feb 10 '20 at 12:01
  • What are `element` and `newElement`? How are `search1` and `search2` being used? – Barmar Feb 10 '20 at 12:02

1 Answers1

2

You can create a regular expression dynamically by changing the spacing in the element string with \s+: that will match newlines also:

var str = `:
    The left lower lung thick parenchymal
    band as seen on prior scan remains not hypermetabolic.  The posterior left 
   lung base tiny  density as seen on prior scan remains not significantly 
   hypermetabolic.
 `;

var element = 'The left lower lung thick parenchymal band as seen on prior scan remains not hypermetabolic';
var newElement = '<p1>The left lower lung thick parenchymal band as seen on prior scan remains not hypermetabolic<p1>';

let regex = RegExp(element.replace(/ +/g, "\\s+"), "g");
str = str.replace(regex, newElement);

console.log(str);

If you want the replacement to also retain the original newlines (assuming you only want to wrap within two <p1>), then use the $& backreference in newElement:

var str = `:
    The left lower lung thick parenchymal
    band as seen on prior scan remains not hypermetabolic.  The posterior left 
   lung base tiny  density as seen on prior scan remains not significantly 
   hypermetabolic.
 `;

var element = 'The left lower lung thick parenchymal band as seen on prior scan remains not hypermetabolic';
var newElement = '<p1>$&<p1>';

let regex = RegExp(element.replace(/ +/g, "\\s+"), "g");
str = str.replace(regex, newElement);

console.log(str);

Remark:

It is not clear what <p1> stands for. If this is supposed to be an XML tag, then realise that the closing tag should be </p1> with the forward slash.

If your element string has characters that have a special meaning in regexes then make sure to escape them. See this Q&A on how to do that.

trincot
  • 211,288
  • 25
  • 175
  • 211
  • 1
    Don't forget to escape the non-whitespace parts of the string, so that `$` and such don't have their regex meaning. Escape function question: https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript Probably the easiest way is `let regex = new RegExp(element.split(/\s+/).map(escapeFunction).join("\\s+");` – T.J. Crowder Feb 10 '20 at 12:19
  • ...er, but not missing the `)` and flags. :-) – T.J. Crowder Feb 10 '20 at 12:24