Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regular expression to hyphenate long words

Posted on 2007-11-18
13
Medium Priority
?
531 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
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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

636 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