473,766 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2216
"Anat" <an********@ver int.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/rasterTriangleD OM.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
2803
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 string literals :( my code: -----------------------------------------------------------------------------
5
2901
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 do it. The problem we have is that if we use an apostrophe when writing a product description or product title in the back office of our site, when that data is then used or carried through to another part of the site eg. a live product page...
7
25791
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, comma, hyphen, etc... Is there a regex that does the trick? Thanks, Anat.
28
6755
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 in general be done with one argument functions? Why would one want to do it anyway, other than showing that one knows and to save two characters?
6
4021
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 list of all occurring punctuation marks with its frequency printed next to it. eg 2 hyphens 3 commas
4
4793
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 give a false negative dictionary response... so if the user types in: i need uppercase to become lowercase too what i would like is to remove commas, periods, exclamation points and question marks and apostrophes from the strings before it...
6
4175
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, the output would also need to display how many occurances of each there are (such as ?, ., ,, ..., ! -, ;, : e.t.c) Thanks in advance, Gary
2
2274
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, ampersands because Oracle will not allow this to happen. I find it hard to believe that a database as powerful as Oracle does not have a way to do this? Any helpful information I can share with the developers would be greatly appreocated. The text...
6
2929
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 comma, a dot, ... How can I do this? Thank You, Miguel
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.