-1

I am trying to highlight sentences that contain a given word. Let's say for this example that the word is "lorem". To me a sentence can end multiple ways, for example, it can end with one of the following characters: "," "!" "." "?". This can be done very easy with the following regex:

/\b((?!=|\,|\.).)+(.)\b/gim

Previous I used /^.*lorem.*?(\.|\!|\?)/gim to match sentences containing the word "lorem". But this did not work most of the time as expected. Is there any way I can use the new regex that separates sentences to only match sentences with a given word?

Just a heads up. I understand that this could be accomplished using javascript functions like replace. However, this is not an option. Our custom system, where this regex is going to be used only accepts regex as input.

Vera Perrone
  • 369
  • 1
  • 15
  • Try `/[^=!?,.]*?\blorem\b[^=!?,.]*/g` – Wiktor Stribiżew Dec 26 '18 at 22:52
  • This sort of task doesn't seem like something where the ideal solution is to include the target word directly in the regular expression. You might want to use [`replace()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) with the optional transform function callback. – Patrick Roberts Dec 26 '18 at 22:57
  • @PatrickRoberts yeah, but in this case It must be a regex. It does not need to be perfect but it is the only thing the system accepts. – Vera Perrone Dec 26 '18 at 23:00
  • @WiktorStribiżew This seems to be working quite well. Is there a way I can make the match skip spaces and always start with a letter or number? – Vera Perrone Dec 26 '18 at 23:01
  • 1
    Can you tag whatever "system" this is you're referring to? It's helpful to know what it is you're working with, since I just wasted 5 minutes writing an answer I didn't even know until just now was invalid. – Patrick Roberts Dec 26 '18 at 23:01
  • @PatrickRoberts I will edit the question with this info, it is a custom system that we use in our company. Sorry for your wasted time :( – Vera Perrone Dec 26 '18 at 23:03
  • Not sure I understand what you mean. Please provide some examples. Also, try adding word boundaries at both ends of that regex. `/\b[^=!?,.]*?\blorem\b[^=!?,.]*\b/g` – Wiktor Stribiżew Dec 26 '18 at 23:07
  • @WiktorStribiżew For example: " Vivamus quis tempor lorem, sed eleifend risus." is a valid match. However, it starts with a space. I want to always start a match with a letter or number. – Vera Perrone Dec 26 '18 at 23:10
  • @trincot I am talking about the space before the V. – Vera Perrone Dec 26 '18 at 23:14
  • Can you explain what that space does to the match or not match of lorem? – trincot Dec 26 '18 at 23:16
  • Is it OK to use capture groups in your (unidentified) system or can it only handle complete matches without distinguishing the capture group of interest? – trincot Dec 26 '18 at 23:19
  • @trincot quite sure it can handle any regex. As long as no JS is required. – Vera Perrone Dec 26 '18 at 23:21
  • But I mean, can it highlight only capture group 2 of a match? – trincot Dec 26 '18 at 23:21

1 Answers1

1

You can dynamically construct your RegExp from a target string using this function:

function sentenceWith (word) {
  return new RegExp(String.raw`(?:[a-z\d][^=!?,.]*?|)\b${word}\b[^=!?,.]*`, 'gi');
}

This should be like Wiktor Stribiżew's suggestion except starting matches always begin with letter or number. It assumes that the input is alphanumeric string. If the input word can contain special characters, you should sanitize it using the answer in Is there a RegExp.escape function in Javascript? before feeding it to the constructor.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153