1

I have this regex expression working as expected on Java environment, but i am unabled to make it work properly on Javascript:

/^(?:select\s+)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*\,\s*)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*from\s+)([\w\d\.]+)\s*([\w\d]+)?$/g

This expression should match these kind of input lines:

select a.id, a.name from public.blah a

select a, a.name from public.blah a

select id, name from blah

a.id, name from brah a

from blah a

And for the groups I expect:

1: first projection alias (a) if any

2: first projection attribute (id) if any

3: second projection alias (a) if any

4: second projection attribute (name) if any

5: relation name (public.blah)

6: relation alias (a) if any

The regex is matching on Javascript, but it just can't fetch the groups. There must be something i need to change on this to make it work.

var optionsPattern = /^(?:select\s+)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*\,\s*)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*from\s+)([\w\d\.]+)\s*([\w\d]+)?$/g;

var text = 'select id, text from options.test';

var match = text.match(optionsPattern);

if(match) {
  alert(match[0]); // select id, text from options.test
  alert(match[1]); // undefined 
}

Do you guys have a clue of what needs to be changed on this?

Thank you!

1 Answer 1

2

Remove the g flag. It is changing your regex from "match and return subpatterns" to "search and return all matches"

Sign up to request clarification or add additional context in comments.

2 Comments

Works! I was blind! Thank you.
Anyway i had to make a small change because the first letter of the attributes were being considered the alias, so in the end it is: <BR/>/^(?:select\s+)?(?:(?:([\w\d]+)\.)?([\w\d]+))?(?:\s*\,\s*)?(?:(?:([\w\d]+)\.)?([\w\d]+))?(?:\s*from\s+)([\w\d\.]+)\s*([\w\d]+)?$/

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.