471,887 Members | 1,191 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

regExp and special characters

I've been crunching on this all afternoon:

I need to stop text from wrapping at a special character....if you set the innerHTML of some given element without setting the width of said element, it will set the size of the element to the width of the largess single word and wrap at white spaces....I've gotten that taken care of with   when I don't want to wrap, but if you preset the size of the element to let the text wrap at the end of the element, it will also wrap at special characters (hyphen for example) and I'm not sure how to stop that. The method I am currently using seems like overkill, but let me know if you have any thoughts:

I fist replace all the white space in the string with non breaking spaces
Expand|Select|Wrap|Line Numbers
  1. var str=a.content.replace(/ /g,' ');
I then set the innerHTML of the element and append it, this gives me my offsetWidth accurately. Next I do this:

Expand|Select|Wrap|Line Numbers
  1. if(popup.Content.offsetWidth>200){
  2.     var tmpStr=a.content
  3.     var tmpTxt=tmpStr.split(/(\s\S+[^a-zA-z0-9\s]+\S+)/g)
  4.     var newString=''
  5.     for(i in tmpTxt){
  6.         if((i%2)!=0){
  7.             newString+='\n\r'+tmpTxt[i].substring(1);
  8.         }else{newString+=tmpTxt[i]}
  9.     }
  10.     popup.Content.innerHTML=newString
  11.     popup.Content.style.width=200+'px'
  12.     popup.appendChild(popup.Content)
  13.             }
it works, but like I said....it seems like there should be a better way
Oct 21 '08 #1
0 1014

Post your reply

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.