nathanr


function trim(strin, advancedtrim) {
/* nathanr: trim function for ecmascript, add in second param of true to trim all known spaces */
strin = strin.replace(/^s+|s+$/g, ''); //STANDARD

REGEX

  whitespace
if(advancedtrim) {
strin = strin.replace(/^[su0020]+|[su0020]+$/g, ''); //SPACE
strin = strin.replace(/^[su00A0]+|[su00A0]+$/g, ''); //NO-BREAK SPACE
strin = strin.replace(/^[su1361]+|[su1361]+$/g, ''); //ETHIOPIC WORDSPACE
strin = strin.replace(/^[su1680]+|[su1680]+$/g, ''); //OGHAM SPACE MARK
strin = strin.replace(/^[su2002]+|[su2002]+$/g, ''); //EN SPACE
strin = strin.replace(/^[su2003]+|[su2003]+$/g, ''); //EM SPACE
strin = strin.replace(/^[su2004]+|[su2004]+$/g, ''); //THREE-PER-EM SPACE
strin = strin.replace(/^[su2005]+|[su2005]+$/g, ''); //FOUR-PER-EM SPACE
strin = strin.replace(/^[su2006]+|[su2006]+$/g, ''); //SIX-PER-EM SPACE
strin = strin.replace(/^[su2007]+|[su2007]+$/g, ''); //FIGURE SPACE
strin = strin.replace(/^[su2008]+|[su2008]+$/g, ''); //PUNCTUATION SPACE
strin = strin.replace(/^[su2009]+|[su2009]+$/g, ''); //THIN SPACE
strin = strin.replace(/^[su200A]+|[su200A]+$/g, ''); //HAIR SPACE
strin = strin.replace(/^[su200B]+|[su200B]+$/g, ''); //ZERO WIDTH SPACE
strin = strin.replace(/^[su202F]+|[su202F]+$/g, ''); //NARROW NO-BREAK SPACE
strin = strin.replace(/^[su205F]+|[su205F]+$/g, ''); //MEDIUM MATHEMATICAL SPACE
strin = strin.replace(/^[su2408]+|[su2408]+$/g, ''); //BACKSPACE SYMBOL
strin = strin.replace(/^[su2420]+|[su2420]+$/g, ''); //SYMBOL FOR SPACE
strin = strin.replace(/^[su3000]+|[su3000]+$/g, ''); //IDEOGRAPHIC SPACE
strin = strin.replace(/^[su303F]+|[su303F]+$/g, ''); //IDEOGRAPHIC HALF FILL SPACE
}
return strin;
}


we all need to trim - and trim pro

perl

 y!

perkiset

How often do you find that you need the Advanced portion? My personal trim looks like the first line of your code and seems to be fine... is this more of an international concern or...? when do you see those characters?

nathanr

Advanced portion, not often mainly with different char sets or content that's been c+p'd from word documents; hence why I made it optional. However the last thing you want to be doing is trying to work out that your whitespace didn't trim because it's got a chinese ideographic space instead of a normal one.

perkiset

Aaahhh, the "paste your doc here" option, clean and post, I get it.


Perkiset's Place Home   Politics @ Perkiset's