3

How can I match a field in the MySql-Database with a regular expression that should be null or a defined string? In Javascript the expression would look like this (and in JS it works) /(.{0}|^ADO$)/

1
  • You cannot do this (directly) in JavaScript. JavaScript converts the object you are matching on to string, thus if you are matching on some var which is null, you will be matching on the string null. The regex cannot tell the difference between a null var and a var containing the string null. Note also that /(.{0}|^ADO$)/ will match any string as .{0} is the same as the empty expression which always matches. Your expression is equal to /(|^ADO$)/ which is almost the same as /(^ADO$)?/. Commented Jun 13, 2012 at 14:19

2 Answers 2

8

It might be clearest to use IFNULL so you can explictly indicate how you want to handle NULLs (I'm always looking for ways to make code easier to understand, rather than more tricksy). Perhaps something like this:

SELECT * from table where IFNULL(field, '') REGEXP 'whatever';
Sign up to request clarification or add additional context in comments.

Comments

0

Use IS NULL, like this:

SELECT * FROM table WHERE (field IS NULL OR field = 'ADO')

1 Comment

As a plain query this would be the right way to do it, you're right, but the problem I have is doing this within a regular expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.