-1

I'm trying to highlight a specific paragraph in a large body of text using regex. The issue is the text I need to highlight has had some special characters removed, so I'm having trouble writing a regex to find the match in the body. I started by pulling out all the whole words from the string I need to find, like so:

  • Starting string: "Hello, this is my search string"
  • Resulting string: "Hello this is my search string"

I'm now trying to match the resulting string in the body, but since the body still has some special characters (which I can't remove for display purposes) I am having difficulty with the regex.

  • Body: "This is the body, where you can find lots of special characters. Hello, "this" is my search string without the correct punctuation."

How can I match this sentence? Hello this is my search string and ignore the extra characters in the body?

lamills526
  • 11
  • 2

1 Answers1

0

Try this [^\w\d\s] - here is example

You can use following code to find index of "spacial characters" (as you describe in comment below question "anything that is not a number or a letter", I guess that "space" is not special too) appear:

let str = 'Body: "This is the body, where you can find lots of special characters. Hello, "this" is my search string without the correct punctdsuation."'

let re = /[^\w\d\s]/g;

while ((match = re.exec(str)) != null) 
{
    console.log("match found at " + match.index);
}

Here is working EXAMPLE

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • I don't want to modify the body to do the match. I need to keep the special characters in because they are being displayed. For example, the body may contain "$3" and I don't need to strip that out. – lamills526 May 16 '18 at 21:28
  • @lamills526 You can first make copy of string - and then remove characters in one copy only. – Kamil Kiełczewski May 16 '18 at 21:31
  • If I make a copy, then find a match, I won't be able to highlight in the original body. Which is my main goal – lamills526 May 16 '18 at 21:34