leading and trailing spaces: trim

Post Reply
ArielRauch
Advanced member
Posts: 230
Joined: Thu Aug 07, 2014 10:04 am

leading and trailing spaces: trim

Post by ArielRauch »

Hi,
I somehow forgot how to trim a string.
str.trim() does not work.

Thank you in advanced,

Ariel
loicaigon
Member
Posts: 147
Joined: Wed Jul 10, 2013 10:22 am

Re: leading and trailing spaces: trim

Post by loicaigon »

Hi,

No trim function in Switch Javascript Object Model but it's pretty easy to get this result with a replace call:

Code: Select all

var str = "      foo        "; 
">"+str.replace(/(^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$)/g, "" )+"<" //>foo<
HTH
Loic
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: leading and trailing spaces: trim

Post by gabrielp »

loicaigon wrote:No trim function in Switch Javascript Object Model but it's pretty easy to get this result with a replace call:
Thanks for sharing this. That is very helpful.
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
ArielRauch
Advanced member
Posts: 230
Joined: Thu Aug 07, 2014 10:04 am

Re: leading and trailing spaces: trim

Post by ArielRauch »

I agree with Gabriel - very helpful.

Thank you very much

Ariel
loicaigon
Member
Posts: 147
Joined: Wed Jul 10, 2013 10:22 am

Re: leading and trailing spaces: trim

Post by loicaigon »

My pleasure. Be aware though that it will only remove trailing and leading "spaces". Something like "hey you" won't be considered with previous regular expression but I was sticking to the idea of trimming the string.
For the ones looking for a more global replacement like " he llo " to "hello", you may prefer to use :

Code: Select all

str.replace (/[\s\uFEFF\xA0]+/g, "")
User avatar
andrea mahoney
Newbie
Posts: 19
Joined: Tue Jan 25, 2011 8:53 pm

Re: leading and trailing spaces: trim

Post by andrea mahoney »

You can use a Switch Variable to do this with norm or trim from the Variable options:

job.getVariableAsString("[Job.NameProper:Space=\"norm\"]");
Post Reply