By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Regular expression from JavaScript

P: n/a
Hi

I need to convert the following string:

!Hello World

to:

*Hello World*

with JavaScript and regular expression. Anyone up for a little help?

Regards,
Thomas
Sep 10 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Actually it would be nice to be able to convert:

!any string

to:

<h1>any string</h1>

as well. So "any string" can be ... ehm any string and the ! should be
converted to <h1at the beginning of the line and </h1at the end.

Regards,
Thomas
Sep 10 '06 #2

P: n/a
Thomas Ardal wrote on 10 sep 2006 in comp.lang.javascript:
Actually it would be nice to be able to convert:

!any string

to:

<h1>any string</h1>

as well. So "any string" can be ... ehm any string and the ! should be
converted to <h1at the beginning of the line and </h1at the end.
t = t.replace(/^!(.*)/,'<h1>$1</h1>')

Next time first try it yourself.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 10 '06 #3

P: n/a

Thomas Ardal написав:
Actually it would be nice to be able to convert:

!any string

to:

<h1>any string</h1>

as well. So "any string" can be ... ehm any string and the ! should be
converted to <h1at the beginning of the line and </h1at the end.

Regards,
Thomas
str=str.replace(/\!([^\n\r])+/g,'<h1>$1</h1>')

Sep 10 '06 #4

P: n/a


Well
'!any string here'.replace(/^([^$]+)$/g, '<h1$1 <\/h1>');
Danny
Sep 10 '06 #5

P: n/a
Danny wrote on 11 sep 2006 in comp.lang.javascript:
Well
'!any string here'.replace(/^([^$]+)$/g, '<h1$1 <\/h1>');
Two problems:

1 you did not quote, which on usenet is a sin, as we won't know what you
are reacting upon.

2 why the extra spaces and not just '<h1>$1<\/h1>'

3 why the /g here?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 11 '06 #6

P: n/a
Evertjan. wrote on 11 sep 2006 in comp.lang.javascript:
Danny wrote on 11 sep 2006 in comp.lang.javascript:
> Well
'!any string here'.replace(/^([^$]+)$/g, '<h1$1 <\/h1>');

Two problems:

1 you did not quote, which on usenet is a sin, as we won't know what you
are reacting upon.

2 why the extra spaces and not just '<h1>$1<\/h1>'

3 why the /g here?
4 I cannot count!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.