?
Solved

Regular expression to hyphenate long words

Posted on 2007-11-18
13
Medium Priority
?
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +2
13 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 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 600 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 1200 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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

770 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