Right all I am back on my project where i need to ...
# suitescript
k
Right all I am back on my project where i need to use
regexp
to remove special characters and only leave me with alphanumeric characters. I am using
/\W/g as the .replace
but i am getting inconsistencies when using this. Does anyone have another way or a prebuilt function to do this task?
s
You can try this .replace(/[^a-zA-Z0-9 ]/g, "") @karlenigma
k
Does that mean replace all except alphanumeric?
j
@karlenigma, yeah. "\W" includes the "_" character. What samyt gave you will get everything but alphanumeric.
k
OK Great thanks.
OK so @jarens Can you explain this? I have a value which i check using .test with the regexp. Then I create my new value with .replace. But the value does not get created and on test it doesnt see that it has a
-
in there.
My value is CS-52
j
var regex = RegExp(/[^a-zA-Z0-9]/g) var x = "CS-52"; console.log(regex.test(x)) if(regex.test(x)){ x = x.replace(/[^a-zA-Z0-9]/g,''); console.log(x); }
So this is what you are doing but the test is coming back false? I just ran this through codepen and it appears to work properly.
k
Yeah but i am running it on a Map/Reduce.
Some work some don,t
@jarens Have a look at this. It's just inconsistent.
Copy code
var specialchars = /[^a-zA-Z0-9 ]/g;
            if (specialchars.test(opn)) {
                var altOPN = opn.replace(/[^a-zA-Z0-9 ]/g, '');
            }
the code
j
That's crazy. I haven't a clue...
k
It has been bugging me for weeks and weeks.
the map process takes literally like 1 second and creates 80 records but the above happens.
s
Are you checking first if the special charcter is present or jot?
k
Yep
See code above
s
Ok
Remove if condition and try
k
OK i'll try that but need the if to work.
s
But why? Its replace if it has some special charcter or else not
k
Yes but I don't need duplicates.
Right i have tested without the IF statement and works except for a few where it is leaving the space character in there.
s
Your reg exp is [^a-zA-Z0-9] ?? Rite?
k
opn.replace(/[^a-zA-Z0-9 ]/g, '');
s
Does the space after 9 makes any diffr?
k
well it has the space in my code.
I could remove it.
s
Can you try it?
k
Right so that is now removing the space.
Still dont get why it wont work when using a tet.
s
Cool
But i m still wondering why you are using test over there?
k
I might just do a work around and Set a IF statement to compare the values before using it to create a new record.
s
Yes you can do that!
k
Something like this??
Copy code
var opn = contextobject.values["GROUP(name)"]; // OPN value from Old OPN Record
            var altOPN = opn.replace(/[^a-zA-Z0-9]/g, ''); // OPN value without any special characters
            if (opn === altOPN) {
                altOPN = '';
            }
s
Yes its looks good!!
k
bit weird that the IF statement with the .test does not work.
s
Check what is the test returning or try using === true
j
@karlenigma I've run into this bug. Don't use the
g
flag at the end of your regular expression. The
g
flag is for repeatedly testing the same string with the same regular expression.
When you execute a regular expression with the
g
flag it changes the internal state of the regular expression object. The Rhino engine that runs your javascript on the server has an """optimization""" that reuses a regular expression object when you declare a new one with the same pattern. So even though you have a "new"
/[^a-zA-Z0-9]/g
, it is using the same internal state as the last time you used
/[^a-zA-Z0-9]/g
. This is different behavior than you'll see in your browser.
k
What would I out instead or just remove the g
j
Just don't use the
g
flag. It's for testing the same string with the same regexp, which it doesn't seem like you're doing.
k
OK so no g got it. Will test on Monday.
👍 1
s
@jkabot great !! Something new I learned