Regular expression from JavaScript

T

Thomas Ardal

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
 
T

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 <h1> at the beginning of the line and </h1> at the end.

Regards,
Thomas
 
E

Evertjan.

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 <h1> at the beginning of the line and </h1> at the end.

t = t.replace(/^!(.*)/,'<h1>$1</h1>')

Next time first try it yourself.
 
S

scriptguru

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 <h1> at the beginning of the line and </h1> at the end.

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

Evertjan.

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?
 
E

Evertjan.

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top