Solved

Regular expression to hyphenate long words

Posted on 2007-11-18
13
514 Views
Last Modified: 2012-05-05
I have a website where user-driven content is being displayed in columns. Words longer than, say, 30 characters are pushing out the column widths. This includes URLs that users enter, which is not uncommon.
I need a regex that will find these long words and insert a hyphen after the nth character. This will render URLs useless, but that's OK, because the content in the columns is just summary content that links to a full version that won't need to be hyphenated..

Thanks!
0
Comment
Question by:azaram
  • 3
  • 3
  • 3
  • +2
13 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 20310745
0
 

Author Comment

by:azaram
ID: 20310769
Hi ozo,
I'm not really sure what that was that you sent.. It appears to be a word-manipulation library for perl?
I'm coding in ASP and would prefer a regex solution if anyone has one.
0
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 150 total points
ID: 20310801
Hello azaram,

you could use:

var str = "abcdefghijklmnopqrstuvwxyz";

document.write(str.replace(/(\S{10})(\S)/g, "$1,$2"));

I used \S{10} in this example, but you could use \S{30} ofcourse.

Regards,

Roonaan
0
 
LVL 84

Expert Comment

by:ozo
ID: 20310831
it is a regular expression library to find the hypenenation points in English.
There's another library that interprets TeX patterns to determine hypenation points
http://www.ccl.net/cca/text-processing/tex/latex/polish/hyphen.english
0
 
LVL 17

Accepted Solution

by:
gops1 earned 300 total points
ID: 20311466
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 20311472
Is it an idea to just quote the appropriate code snippet along with the url? This would preserve the snippets in the paq when the external website is changed or removed. It might be easier on azaram as well.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20312100
@Roonan:

var str = "abcdefghijklmnopqrstuvwxyz";
document.write(str.replace(/(\S{10})(\S)/g, "$1<wbr>$2"));
0
 
LVL 17

Expert Comment

by:gops1
ID: 20312155
Change <wbr> to <br>
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 20312166
That's not necessairy.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20312202
gops: your own suggestion uses  <wbr>


so why not?
 var strSoftHyphen = (navigator.userAgent.toLowerCase().indexOf("applewebkit") > -1 || document.all) ? "&shy;" : "<wbr/>"; // use soft-hyphen for IE and Opera which are known to implement it correctly

Open in new window

0
 
LVL 17

Expert Comment

by:gops1
ID: 20312209
<wbr> is not supported by all browsers. I can see this is not working in my browser (IE6)
0
 

Author Comment

by:azaram
ID: 20317217
Thanks, I have a good solution now.

gops1 got most points as this solution did exactly what I wanted. A simple regex would have been better, but I think this one was more comprehensive. I did leave it open for JS solutions. I've hacked it for now by inserting <script lang....>document.write(SoftWrap('kdflksdjflksdjflksdf'),20)</script> wherever it's needed for now. That works fine, but a bit stupid/verbose.. next step I'll probably wrap each  bit of user-driven content in a <div id="usercontent1023"> for example and have a script that runs through all divs that match usercontent.... and transform them.

Roonaan, gave you part marks for the Regex, which is specificalyl what I was asking for, but in practice I couldn't get it working properly in classic ASP with the regex object and regex replace. I put that down more to my lack of regex knowledge and that I didn't play with it too much because the softwrap JS worked fine..

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20318415
Gops: your suggestion uses <wbr> or &shy; depending on browser...
But what platform?

this works  on IE6 on windows:

<span style="width:100px">test<wbr>testtest<wbr>testtest<wbr>testtest<wbr>testtest<wbr>testtest<wbr>test</span>
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now