Regular expression from JavaScript

Discussion in 'Javascript' started by Thomas Ardal, Sep 10, 2006.

  1. Thomas Ardal

    Thomas Ardal Guest

    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
     
    Thomas Ardal, Sep 10, 2006
    #1
    1. Advertising

  2. Thomas Ardal

    Thomas Ardal Guest

    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
     
    Thomas Ardal, Sep 10, 2006
    #2
    1. Advertising

  3. Thomas Ardal

    Evertjan. Guest

    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.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Sep 10, 2006
    #3
  4. Thomas Ardal

    Guest

    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>')
     
    , Sep 10, 2006
    #4
  5. Thomas Ardal

    Danny Guest

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


    Danny
     
    Danny, Sep 11, 2006
    #5
  6. Thomas Ardal

    Evertjan. Guest

    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)
     
    Evertjan., Sep 11, 2006
    #6
  7. Thomas Ardal

    Evertjan. Guest

    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)
     
    Evertjan., Sep 11, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,382
  2. Dot net work
    Replies:
    2
    Views:
    7,144
    Peter Blum
    May 10, 2004
  3. Martin
    Replies:
    0
    Views:
    701
    Martin
    May 30, 2005
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    883
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,048
    vforvikash
    Dec 29, 2008
Loading...

Share This Page