I am trying to make my regex work across multiple lines and "m" didn't seem to work either. So, my regex is working for 1st line and noT for the following lines.
Asked
Active
Viewed 148 times
0
-
An additional note: the **m** flag is used for making the "beginning of line" (`^`) and "end of line" (`$`) characters match those in *each* line. Without it these will match the very beginning and the very end of the string/value. – inhan Oct 09 '12 at 03:07
1 Answers
2
You can skip the match
part and just do it all in one step:
> "the *text* is to be replaced \n by *text*".replace(/\*([\s\S]*?)\*/g, '<i>$1</i>');
"the <i>text</i> is to be replaced \n by <i>text</i>"
.
matches any character, but it excludes newlines.[\s\S]
matches any character including newlines.I changed your search regex to
\*([\s\S]*?)\*
, which non-greedily matches the stuff between the asterisks.The replacement string is
<i>$1</i>
.$1
is replaced with the contents of the first capturing group, which is your text.
Also, because it looks like you're trying to convert Markdown to HTML, try using a pre-made JS converter: http://www.showdown.im/
You can use it like this:
var str = "the *text* is to be *replaced \n by* *text*";
alert(str.replace(/\*([\s\S]*?)\*/g, '<i>$1</i>'));

Blender
- 289,723
- 53
- 439
- 496
-
You mean? var str="the *text* is to be replaced \n by *text*"; alert(newStr.replace(/\*(.*?)\*/g, '$1'); should work?! It doesn't... :/ i'm totally new to regex... this doesnt alert anything at all.. – Watchful Protector Oct 09 '12 at 03:08
-
Yep, that's it (but change `newStr` to `str`). I'm testing my JS in a console so I cram everything into one line. – Blender Oct 09 '12 at 03:09
-
That does work. However, I was told that the same regex should be used for a string like "* a\n1 *" and it should do the same. But, it doesn't. Can you give me some inputs on this please? And also, \n should be displayed as part of new string. It shouldn't disappear – Watchful Protector Oct 09 '12 at 03:18
-
@user1727587: `alert()` is a cruddy debugging tool. Don't use it. Grab Firebug for Firefox or Chrome's built-in Inspector and use `console.log()` instead. As for the newline, see my edit. – Blender Oct 09 '12 at 03:22
-
It works for * a \n 1* as well. But, the problem that still persists is that of \n being replaced by a new line. I would like to display the \n. – Watchful Protector Oct 09 '12 at 03:43
-
-
Doesn't display in console.log either. I'm trying it with both. But, even in console.log it displays the part after \n in new line and doesn't display \n. – Watchful Protector Oct 09 '12 at 03:58
-
@user1727587: The line break is the newline being rendered. Type `a\nb` into the console. – Blender Oct 09 '12 at 04:01
-
@user1727587: The line break is there, but it's being displayed as literally a line break. When you write `\n`, it isn't ```\``` + `n`. `\n` is *one* character that is rendered as a line break. – Blender Oct 09 '12 at 04:18
-
Hmm.. Okay. So, that means it is actually a part of what is being displayed. But, we don't see it! Great. Thank you so much for your help. :) – Watchful Protector Oct 09 '12 at 04:30
-
@user1727587: Yep, it's just a display issue. See my question here: http://stackoverflow.com/questions/12792732/displaying-control-characters-in-chromes-console – Blender Oct 09 '12 at 04:32
-
I copy pasted "a\nb" in firefox's console editor. But, even that doesn't display the \n, like you say in that questions of yours. How do I get the \n to display in firefox atleast? – Watchful Protector Oct 09 '12 at 04:45
-
@user1727587: I typed that into the console and took a screenshot. Not sure how else to help you there. – Blender Oct 09 '12 at 04:48