Page 1 of 1

Javascript regex help please!

Posted: Tue Jan 13, 2015 8:23 pm
by Mardonis
I have the following code below in the second function that I am trying to pass jslint. The error is saying "Insecure '.'. (Column: 50). I'm not good with regex and trying to understand it but not having any luck. Any help is greatly appreciated.

Code: Select all

function round2(number) {
    return Math.round(number * 100) / 100;
}

Code: Select all

function round2str(number) {
// Force two decimals only:
    return round2(number).toString().replace(/(\...).*/, '$1');
}

Thanks,
Mardonis

Re: Javascript regex help please!

Posted: Tue Jan 13, 2015 10:53 pm
by Jackolantern
The code appears to be working to me. You have to take some of the jsLint warnings with a grain of salt. What it is likely hitting on is that you are taking parameters in a function and replacing them into a string with regex, which could possibly be insecure if you weren't careful about sanitizing user input.

Really, a lot of people have gotten annoyed with jsLint because they feel it has become too opinionated and is trying to force a specific code style rather than catch potential errors. jsHint was made to fill the original purpose of jsLint without getting too preachy.

Re: Javascript regex help please!

Posted: Sat Jan 17, 2015 9:42 pm
by Chris