I have two strings
- abc
\r\ndef- c
\nde
And I need to find index of string 2 inside string 1.
I can't obviously use indexOf(), so I need something that would work like it, but took different line endings into consideration.
I cannot modify the original string because I need substring index in the original string. If I replace all \r\n with \n it will mess up original indexes, so I would have to restore them somehow.
(FWIW, there's nothing TypeScript-specific about this question. Just JavaScript.)
You can do it by converting the string you're looking for into a regular expression using the alternation
\r\n|\r|\neverywhere it has any of those sequences, and being sure to escape the parts in-between (see this question's answers for that). When you use theexecmethod of the resulting regular expression on the first string, if it matches, the return value (let's call itmatch) will be a match result (enhanced array) with the index available asmatch.indexand the matched text available asmatch[0].Example with TypeScript type annotations commented out: