469,331 Members | 1,896 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

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 969

Post your reply

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

Similar topics

10 posts views Thread by Anand Pillai | last post: by
5 posts views Thread by Syed Ali | last post: by
13 posts views Thread by Phat G5 (G3) | last post: by
7 posts views Thread by arno | last post: by
6 posts views Thread by Christoph | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.