regExp and special characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zaphod42
    New Member
    • Oct 2008
    • 55

    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
    Code:
    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:

    Code:
    if(popup.Content.offsetWidth>200){
    	var tmpStr=a.content
    	var tmpTxt=tmpStr.split(/(\s\S+[^a-zA-z0-9\s]+\S+)/g)
    	var newString=''
    	for(i in tmpTxt){
    		if((i%2)!=0){
    			newString+='\n\r'+tmpTxt[i].substring(1);
    		}else{newString+=tmpTxt[i]}
    	}
    	popup.Content.innerHTML=newString
    	popup.Content.style.width=200+'px'
    	popup.appendChild(popup.Content)
    			}
    it works, but like I said....it seems like there should be a better way
Working...