String splitting without loosing punctuation marks

Discussion in 'Javascript' started by Anat, May 25, 2006.

  1. Anat

    Anat Guest

    Hi,
    I need a little help on performing string manipulation:
    I want to take a given string, and make certain words hyperlinks.
    For example:
    "Hello world, this is a wonderful day!"
    I'd like the words world & and day to be hyperlinks, therefore
    after my manipulation it should be:
    "Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
    Using split method is not good, because splitting with regex each
    punctuation mark causes the commas and other punctuation marks to
    disappear.
    Instead of displaying
    "Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
    I will display
    "Hello <a href=...>world</a> this is a wonderful <a href=...>day</a>"
    (note that the comma and exclamation mark are gone).
    Any ideas on how I can locate words, replace them but not loose
    punctuation marks on the way?
    Thanks a lot!!!
    Anat.
    Anat, May 25, 2006
    #1
    1. Advertising

  2. Anat

    Lee Guest

    Anat said:
    >
    >Hi,
    >I need a little help on performing string manipulation:
    >I want to take a given string, and make certain words hyperlinks.
    >For example:
    >"Hello world, this is a wonderful day!"
    >I'd like the words world & and day to be hyperlinks, therefore
    >after my manipulation it should be:
    >"Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
    >Using split method is not good, because splitting with regex each
    >punctuation mark causes the commas and other punctuation marks to
    >disappear.
    >Instead of displaying
    >"Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
    >I will display
    >"Hello <a href=...>world</a> this is a wonderful <a href=...>day</a>"
    >(note that the comma and exclamation mark are gone).
    >Any ideas on how I can locate words, replace them but not loose
    >punctuation marks on the way?


    The words are "losing" and "lose", not "loosing" and "loose".
    Split on whitespace and then handle punctuation separately.


    --
    Lee, May 25, 2006
    #2
    1. Advertising

  3. "Anat" <> writes:

    > "Hello world, this is a wonderful day!"
    > I'd like the words world & and day to be hyperlinks, therefore
    > after my manipulation it should be:
    > "Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"


    .... and these words were chosen because they are just before
    punctuation, I guess.

    > Using split method is not good, because splitting with regex each
    > punctuation mark causes the commas and other punctuation marks to
    > disappear.
    > Instead of displaying
    > "Hello <a href=...>world</a>, this is a wonderful <a href=...>day</a>!"
    > I will display
    > "Hello <a href=...>world</a> this is a wonderful <a href=...>day</a>"
    > (note that the comma and exclamation mark are gone).


    If you want to replace parts of a string ... use the replace function:

    string.replace(/\b(\w+)([.,!?])/g, "<a href=\"...\">$1<\/a>$2")

    > Any ideas on how I can locate words, replace them but not loose
    > punctuation marks on the way?


    Replace and keep the punctuation mark in the replacement.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, May 25, 2006
    #3
    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. dew

    Stripping out punctuation marks

    dew, Feb 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    418
    Nathan Sokalski
    Feb 7, 2006
  2. John Ericson
    Replies:
    0
    Views:
    419
    John Ericson
    Jul 19, 2003
  3. Replies:
    2
    Views:
    419
    Peter Koch Larsen
    Apr 13, 2005
  4. Replies:
    2
    Views:
    320
    Peter Koch Larsen
    Apr 13, 2005
  5. Vadim
    Replies:
    1
    Views:
    1,192
    Pascal J. Bourguignon
    Dec 24, 2008
Loading...

Share This Page