Javascript regex help please!

C++, C#, Java, PHP, ect...
Post Reply
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Javascript regex help please!

Post 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
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Javascript regex help please!

Post 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.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Javascript regex help please!

Post by Chris »

Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Coding”