By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

regExp and special characters

P: 55
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
Share this question for a faster answer!
Share on Google+

Post your reply

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