azaram
asked on
Regular expression to hyphenate long words
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!
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!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
There's another library that interprets TeX patterns to determine hypenation points
http://www.ccl.net/cca/text-processing/tex/latex/polish/hyphen.english
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
@Roonan:
var str = "abcdefghijklmnopqrstuvwxy z";
document.write(str.replace (/(\S{10}) (\S)/g, "$1<wbr>$2"));
var str = "abcdefghijklmnopqrstuvwxy
document.write(str.replace
Change <wbr> to <br>
That's not necessairy.
gops: your own suggestion uses <wbr>
so why not?
so why not?
var strSoftHyphen = (navigator.userAgent.toLowerCase().indexOf("applewebkit") > -1 || document.all) ? "­" : "<wbr/>"; // use soft-hyphen for IE and Opera which are known to implement it correctly
<wbr> is not supported by all browsers. I can see this is not working in my browser (IE6)
ASKER
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(So ftWrap('kd flksdjflks djflksdf') ,20)</scri pt> 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..
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(So
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..
Gops: your suggestion uses <wbr> or ­ depending on browser...
But what platform?
this works on IE6 on windows:
<span style="width:100px">test<w br>testtes t<wbr>test test<wbr>t esttest<wb r>testtest <wbr>testt est<wbr>te st</span>
But what platform?
this works on IE6 on windows:
<span style="width:100px">test<w
ASKER
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.