I have a text pattern that I am trying to replace in a node.js application. The pattern is:
***
some text
***
It is created in javascript with the following code:
var textblock = "***" + '\n' + 'some text' + '\n' + "***" + 'the rest of the text block'
The following regular expression works in regexpal and seems correct to me:
\*{3}\n.+\n\*{3}
But when I put it in my javascript code, it fails:
textblock.match(/\*{3}\n.+\n\*{3}/) // returns null
I tested, and even just *{3}\n doesn't seem to work. Am I missing something idiosyncratic about how javascript handles \n ? I've tried /m as well, and I've also tried [\n\r].
Thanks!
UPDATE: turns out that the GitHub API markdown processes issue body text and eliminates newlines. So my regex was correct, but I was wrong about the text I was matching in.
Try changing textblock to t:
var t = "***" + '\n' + 'some text' + '\n' + "***";
alert(t.match(/\*{1}\n.+\n\*{1}/));
(fiddle; removed some * from the regexp to check if it is working properly).
It might be an issue with line-endings. If you match for [\n\r] instead of just \n it works OK.
Here is a fiddle to demonstrate: http://jsfiddle.net/xD8a5/
The text I'm finding and replacing comes from a GitHub issue fetched via API. Turns out that when you put multiple asterisks in a row, they omit the newline chars before and after since they convert it into a graphical line.
The answer was to not require actual newline characters. This worked:
match(/\*{3}[\n\r]*.+[\n\r]*\*{3}/)
Thanks everyone for your help!
Use \\n instead of \n i tried on my code and it is working fine