473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

String splitting without loosing punctuation marks

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.

May 25 '06 #1
2 2198
"Anat" <an********@verint.com> 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 25 '06 #2
Anat wrote:
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>!"


Just curious - why do you want to do this with javascript? Is there a
reason you can't output it this way in the first place?

As for how, I see someone has already provided the same suggestion I
would - split on the spaces.
May 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Xiao Jianfeng | last post by:
Hi, I need to print a long sting, which is two long so it must expand two lines. I know that we can use backslash(\) to explicitly join two lines into a logical line, but this doesn't work for...
5
by: pihkal23 | last post by:
Hi I am not a PHP user; I manage the server/ network for our business. We've employed web developers (chosen because they're friends of the MD) to build us a site and they've used PHP & MySQL to...
7
by: Anat | last post by:
Hi, What regex do I need to split a string, using javascript's split method, into words-array? Splitting accroding to whitespaces only is not enough, I need to split according to whitespace,...
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
6
by: watcher00 | last post by:
Hi I'm a complete newbie at Perl and i was wondering if i can get some help completing an exercise i've come across. I need to count the punctuation marks from a text file and then output a...
4
by: jerger | last post by:
i have a great program now with the help of a member from this site, but i need a little customization to meet the needs of non-english speakers... who might accidendtly type punctuation which would...
6
by: sv5perl | last post by:
I want to ask for some advice on a script that will count all the punctuation marks in a text file, I know it's probably quite a basic thing but I am new to Perl and would really appreciate the help,...
2
by: parnellsquare | last post by:
I am a software tester - the developers I am working with are telling me there is no way to allow users to enter text that includes punctuation marks like apostrophes, quate marks, semi-colons,...
6
by: shapper | last post by:
Hello, I have a string and I need to get as many words possible counting from the beginning but without exceeding 120 characters. I can't break words and I the string shouldn't end with a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.