What does this Javascript statement (regular expression) mean? - javascript

What does the RegEx test for here?
function chksql(){
if (/^\s*(?:delete|drop|truncate|alter)/.test(v)) return false;
}
I just know it's mixed with regular expression, but can't figure out what it means.

it means its checking if v is a string that starts with zero or more white space charcters followed by delete or drop or truncate or alter
so if v were " alter" this would return false.
see docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
I should add that checking against this happening on the client side is a really bad idea. It will be circumvented.

There are a number of good online tools for testing and exploring regular expressions these days.
One I like is debuggex.com. Here's what it displays for your regular expression:
^\s*(?:delete|drop|truncate|alter)
Debuggex Demo
To interpret that, you still need to do a bit of homework like finding out what ^ and \s mean, but the "railroad diagram" helps show what the regular expression is testing for. Just follow the lines to see what it will match. You can also try typing in test strings at the link above to see how it matches (or doesn't match) them.
Another good site is regex101.com. Here's your regular expression there. They give you an English description of what the regular expression is looking for.
Also, heed mkoryak's advice about trying to sanitize SQL on the client!

Related

RegEx false negative with .test()

I'm making a Chrome extension that searches a page for a dollar amount (a number with no more then two decimal places immediately preceded by a "$") then tacks on a bit with how much that value would be in another currency. I found a commonly used regex that matches exactly those parameters.
/^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$/g
so I'm thinking I have a nice headstart. I've only been coding a couple of months and of all the concepts I've encountered, regex's give me the most headache. I test out my shiny new expression with:
var regex = /^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$/g;
var str = "The total it $2.25 Would you like paper or plastic?";
r = regex.test(str);
console.log(r);
and of course that sucker returns false! I tried a few more strings with "2.25" or "$2" or "$2.256" just to be sure and they all returned false.
I am thoroughly stumped. The expression came recommended, I'm using .test() correctly. All I can think of is it's probably some small newbish detail that has nothing to do with regex's.
Thanks for your time.
Your overly complex regular expression is checking the entire string. Remove the ^ and $ which denote the beginning and end of the string, respectively. Then remove the /g flag, which is used to search for multiple matches.
What's wrong with checking for /\$\d+\.\d\d/?
I find http://regex101.com/ to be a helpful resource.

JS regular expression to match imdb url

Can any one tell me what is wrong with this javascript code
"http://www.imdb.com/title/tt2618986/".match("~http://(?:.*\.|.*)imdb.com/(?:t|T)itle(?:\?|/)(..\d+)~i");
When i try this here https://regex101.com/r/yT7bG4/1 it works but not in javascript
The way you create a regular expression in JavaScript is /pattern/flags. The code you are looking for is something along the lines of:
"http://www.imdb.com/title/tt2618986/".match(/http:\/\/(?:.*\.|.*)imdb.com\/(?:t|T)itle(?:\?|\/)(..\d+)/i);
You have to escape all of the / in the regular expression so the / become part of the regular expression instead of indicating the end of it. I would suggest reading this article if you want to learn more about regular expressions in JavaScript.
Also, https://regex101.com/ has a JavaScript option on the left, under the 'FLAVOR' banner, which may help knowing which flags are valid.
You are using pcre(php) flavor in regex101. You should select javascript flavor.
Considers that there is not '~' delimiter in javascript RegExp. This is why your code is not working.
You should write something like:
"http://www.imdb.com/title/tt2618986/".match(/http:\/\/(?:.*\.|.*)\.imdb.com\/(?:t|T)itle(?:\?|\/)(..\d+)/i);
In your case:
/ symbol must be escapes - like this /.
there is not '~' delimiter
Result code with regular expression is:
"http://www.imdb.com/title/tt2618986/".match(/http:\/\/(?:.*\.|.*)imdb.com\/title(?:\?|\/)(..\d+)/i)
p.s. use modifier 'i' to do a case-insensitive search

how to use string searching syntax in javascript

Can someone please explain the syntax of searching through strings? For example, I have this piece of code:
var ok = phone.value.search(/^\d{3}-\d{4}$/);
phone is a variable that is supposed to contain a phone number, and I know from context that this is supposed to make sure the inputted number has the format ###-####, but I don't know what the code within the parenthesis means or how it is evaluated. If someone has a link explaining how to use code like that I would especially appreciate it.
That's a regular expression ( regex ),
Regex One has a good guide on how to use them
Your regex says "beginning with 3 digits, then a "-" then 4 digits"
It's a regular expression, a whole world in itself.
http://www.regular-expressions.info/tutorial.html
It is regex object. The ^ matches the beggining of the string, the \d{3} matches 3 digits, the - matches a dash, the \d{4} matches for digits, and finally the $ matches the end of the string.
What you have there is called a "regular expression" and as you say, they are used to ensure input matches a certain pattern. I recommend you go somewhere like http://www.regular-expressions.info/ for further info rather than re-post data here.

How to invert an existing regular expression in javascript?

I have created a regex to validate time as follows : ([01]?\d|2[0-3]):[0-5]\d.
Matches TRUE : 08:00, 09:00, 9:00, 13:00, 23:59.
Matches FALSE : 10.00, 24:00, 25:30, 23:62, afdasdasd, ten.
QUESTION
How to invert a javascript regular expression to validate if NOT time?
NOTE - I have seen several ways to do this on stack but cannot seem to make them work for my expression because I do not understand how the invert expression should work.
http://regexr.com?38ai1
ANSWER
Simplest solution was to invert the javascript statement and NOT the regex itself.
if (!(/^(([01]?\d|2[0-3]):[0-5]\d)/.test(obj.value))
Simply adding ! to create an if NOT statement.
A regular expression is usually used for capturing some specific condition(s) - the more specific, the better the regex. What you're looking for is an extremely broad condition to match because just about everything wouldn't be considered "time" (a whitespace, a special character, an alphabet character, etc etc etc).
As suggested in the comments, for what you're trying to achieve, it makes much more sense to look for a time and then check (and negate) the result of that regular expression.
As i mentioned in the comment, the better way is to negate the test rather then create a new regexp that matches any non-time.
However, if you really need the regexp, you could use negative lookahead to match the start of something that is not a time:
/^(?!([01]?\d|2[0-3]):[0-5]\d$)/
DEMO: http://regex101.com/r/bD3aG4
Note that i anchored the regexp (^ and $), which might not work with what you need it for.

Match altered version of first match with only one expression?

I'm writing a brush for Alex Gorbatchev's Syntax Highlighter to get highlighting for Smalltalk code. Now, consider the following Smalltalk code:
aCollection do: [ :each | each shout ]
I want to find the block argument ":each" and then match "each" every time it occurrs afterwards (for simplicity, let's say every occurrence an not just inside the brackets).
Note that the argument can have any name, e.g. ":myArg".
My attempt to match ":each":
\:([\d\w]+)
This seems to work. The problem is for me to match the occurrences of "each". I thought something like this could work:
\:([\d\w]+)|\1
But the right hand side of the alternation seems to be treated as an independent expression, so backreferencing doesn't work.
Is it even possible to accomplish what I want in a single expression? Or would I have to use the backreference within a second expression (via another function call)?
You could do it in languages that support variable-length lookbehind (AFAIK only the .NET framework languages do, Perl 6 might). There you could highlight a word if it matches (?<=:(\w+)\b.*)\1. But JavaScript doesn't support lookbehind at all.
But anyway this regex would be very inefficient (I just checked a simple example in RegexBuddy, and the regex engine needs over 60 steps for nearly every character in the document to decide between match and non-match), so this is not a good idea if you want to use it for code highlighting.
I'd recommend you use the two-step approach you mentioned: First match :(\w+)\b (word boundary inserted for safety, \d is implied in \w), then do a literal search for match result \1.
I believe the only thing stored by the Regex engine between matches is the position of the last match. Therefore, when looking for the next match, you cannot use a backreference to the match before.
So, no, I do not think that this is possible.

Categories

Resources