Skip to main content
Participant
April 6, 2011
Answered

wbr every 5 characters in really long string

  • April 6, 2011
  • 2 replies
  • 789 views

I have some users put in really long strings in a text area and when it's displayed the formatting is thrown off because there are no spaces. To fix this I want to insert a wbr every 5 spaces that gives the browser the option to break if it needs to. I have it working in javascript and a version working in coldfusion, but the coldfusion version inserts the wbr every other character. Any idea how to insert a wbr every 5 characters with a REReplace()?

Thanks!

GP

javascript:

<script type="text/javascript" language="JavaScript">

str = '#really_long_text#';

str = str.replace(/(.{5})/g,'$&<wbr/>');

document.write(str)

</script>

ColdFusion:

<cfset str = REReplace(really_long_text, "(.{0})", "<wbr>", "all">

again this does it every other character, but I need it every 5 characters

This topic has been closed for replies.
Correct answer Owainnorth

But you're so close!

<cfoutput>#reReplaceNoCase("abcdefghijklmnopqrstuvwxyz", '(.{5})', '\1 ', 'all')#</cfoutput>

O.

2 replies

Participant
April 7, 2011

The function works almost perfcet... but in the image below you'll see it will break a word it shouldn't if given the change. (Below, it breaks "make")

Any idea how to check if there is a space coming up in the next 7 characters and if not, put a <wbr>?

Here's the code as it's working:

<cfset notes_wbr = REReplaceNoCase("#notes#", '(.{5})', '\1<wbr>', 'all')>

But it does this to the output where "make" is broken into two words:

Owainnorth
OwainnorthCorrect answer
Inspiring
April 7, 2011

But you're so close!

<cfoutput>#reReplaceNoCase("abcdefghijklmnopqrstuvwxyz", '(.{5})', '\1 ', 'all')#</cfoutput>

O.

Participant
April 7, 2011

Thanks a ton!