0

How can i find a php objects property using regex in javascript?

For example, heres some php.

 $obj = new stdClass();
 $obj->name = '';

With the regex, i wish to get name.

I imagine the regex would look for a /\$\w+/ variable followed by ->, or maybe only ->?

The purpose for the regex is to extend some syntax highlighting.

2
  • 4
    Are you trying to parse PHP code with Javascript? What are you trying to accomplish? Commented Dec 23, 2012 at 20:11
  • 3
    Not at all! Im just extending some syntax highlighting. Commented Dec 23, 2012 at 20:22

1 Answer 1

1

\$[a-zA-Z0-9_]+\s*->\s*([a-zA-Z0-9_]+)\s*=\s*([^;]+); will work assuming no quoted semicolons; if you have quoted semicolons, regexes are a bad choice. First group is the variable name and the second is the assignment.

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

2 Comments

For some reason im having trouble with the -> part. If i shorten the code to test what breaks, \$[a-zA-Z0-9_]+\s*- will select $obj-, but '\$[a-zA-Z0-9_]+\s*->' will not select $obj->... Any reason why that could be?
Interesting, i figured out the reason why i have been having trouble with this is because at some point the code is going back and forth between html entities. So $[a-zA-Z0-9_]+\s*-> actually works for me.

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.