What does this Javascript (regular expression) operator mean?

What does the RegEx test do here?

function chksql(){ if (/^\s*(?:delete|drop|truncate|alter)/.test(v)) return false; } 

I just know that this mixes up with regex, but can't figure out what that means.

+6
source share
2 answers

this means checking it if v is a line starting with zero or more space characters, followed by deleting or drop or truncating or changing

therefore, if v were " alter" , this will return false.

see docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

I must add that checking against this happens on the client side - a really bad idea. It will cost.

+5
source

These days there are many good online tools for testing and learning regular expressions.

I like debuggex.com . Here is what it displays for your regular expression:

 ^\s*(?:delete|drop|truncate|alter) 

Regular expression visualization

Demo version of Debuggex

To interpret this, you still need to do your homework, for example, to find out what ^ and \s mean, but the "railway diagram" helps show that it is testing the regular expression. Just follow the lines to see what happens. You can also try entering test lines at the link above to find out how it matches (or doesn't).

Another good site is regex101.com . Here is your regular expression there . They give you a description in English of what the regular expression is looking for.

Also, follow mkoryak's advice on trying to sanitize SQL on the client!

enter image description here

+2
source

Source: https://habr.com/ru/post/955161/


All Articles