The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 05:58:48 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: ECMAScript (JS) Trim!  (Read 4999 times)
nathanr
n00b
*
Offline Offline

Posts: 7


View Profile
« on: October 27, 2007, 12:48:17 PM »

Code:
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(/^[\s\u0020]+|[\s\u0020]+$/g, ''); //SPACE
strin = strin.replace(/^[\s\u00A0]+|[\s\u00A0]+$/g, ''); //NO-BREAK SPACE
strin = strin.replace(/^[\s\u1361]+|[\s\u1361]+$/g, ''); //ETHIOPIC WORDSPACE
strin = strin.replace(/^[\s\u1680]+|[\s\u1680]+$/g, ''); //OGHAM SPACE MARK
strin = strin.replace(/^[\s\u2002]+|[\s\u2002]+$/g, ''); //EN SPACE
strin = strin.replace(/^[\s\u2003]+|[\s\u2003]+$/g, ''); //EM SPACE
strin = strin.replace(/^[\s\u2004]+|[\s\u2004]+$/g, ''); //THREE-PER-EM SPACE
strin = strin.replace(/^[\s\u2005]+|[\s\u2005]+$/g, ''); //FOUR-PER-EM SPACE
strin = strin.replace(/^[\s\u2006]+|[\s\u2006]+$/g, ''); //SIX-PER-EM SPACE
strin = strin.replace(/^[\s\u2007]+|[\s\u2007]+$/g, ''); //FIGURE SPACE
strin = strin.replace(/^[\s\u2008]+|[\s\u2008]+$/g, ''); //PUNCTUATION SPACE
strin = strin.replace(/^[\s\u2009]+|[\s\u2009]+$/g, ''); //THIN SPACE
strin = strin.replace(/^[\s\u200A]+|[\s\u200A]+$/g, ''); //HAIR SPACE
strin = strin.replace(/^[\s\u200B]+|[\s\u200B]+$/g, ''); //ZERO WIDTH SPACE
strin = strin.replace(/^[\s\u202F]+|[\s\u202F]+$/g, ''); //NARROW NO-BREAK SPACE
strin = strin.replace(/^[\s\u205F]+|[\s\u205F]+$/g, ''); //MEDIUM MATHEMATICAL SPACE
strin = strin.replace(/^[\s\u2408]+|[\s\u2408]+$/g, ''); //BACKSPACE SYMBOL
strin = strin.replace(/^[\s\u2420]+|[\s\u2420]+$/g, ''); //SYMBOL FOR SPACE
strin = strin.replace(/^[\s\u3000]+|[\s\u3000]+$/g, ''); //IDEOGRAPHIC SPACE
strin = strin.replace(/^[\s\u303F]+|[\s\u303F]+$/g, ''); //IDEOGRAPHIC HALF FILL SPACE
}
return strin;
}

we all need to trim - and trim properly!
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: October 27, 2007, 12:53:24 PM »

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?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nathanr
n00b
*
Offline Offline

Posts: 7


View Profile
« Reply #2 on: October 27, 2007, 12:58:37 PM »

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.
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: October 27, 2007, 01:16:48 PM »

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

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!