A. DC
A. DC

Reputation: 1463

Regex that matches quoted and unquoted same string

I've a concatenated file in js where I have functions starting with underscore and some other who are not. In this file I'd like to match only functions starting with underscore regardless if the function is quoted or not.

How can I do that?

This is matching only one or another, not both in: test._function1 = function(){} "ng-click="test._function1()""

/(_)[^" ]+|_/

Upvotes: 1

Views: 581

Answers (1)

Retic
Retic

Reputation: 199

You Definitely Require the /g Modifier.

The Regex you Provided Will Match all _ Characters in your string this could cause some issues. Also its not needed to add another group.

Instead of using this.

/(_)[^" ]+|_/g

you could use this

(_)[^" ]+/g

Live demo: https://regex101.com/r/sB7kX2/1

Lastly it might be better to add a better prefix if Possible. This Should limit the chances of untended matches.

(_myFunction)[^" ]+/g

Upvotes: 4

Related Questions