This is a noob question
Suppose I search a string S for a pattern P. Now I would like to display a substring of the string, which surrounds P. The substring should be only one line (i.e. N characters) and contain whole words. How would you code it in JavaScript?
For example:
Let S = “Hello world, welcome to the universe”, P = “welcome”, and N = 15. The naive solution gives “ld, welcome to ” (adding 4 chars before and after P). I would like to “round” it up to “world, welcome to”.
Can regular expressions help me here?
Here is the regular expression you wanted:
Explanation:
What you are trying to match is actually
without the spaces before and after, therefore:
Applying: