[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular expression to hyphenate long words

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

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
Independent Software Vendors: 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 85

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

Industry Leaders: 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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

830 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