elmazzun
elmazzun

Reputation: 1086

Javascript regex: ignore every letter after backslash

I want to retrieve the free variables in a lambda expression. For example

\z.\x.(xy)

, where "\" stand for the Lambda symbol: through a regex, I need to get all the letters which don't follow a backslash. In my example, the free variables would be

{y}

since "y" is the only variable not bounded to "\". How could I do that? Thanks in advance.

Upvotes: 1

Views: 87

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382130

You can use /\\(\w+)/g and iterate with exec :

var r = /\\(\w+)/g, m,
    s = "\\z.\\x.(xy)";
while (m = r.exec(s)) console.log(m[1]);

It logs "z" then "x".

Demonstration


To answer the new question:

To get the names not following a \, you may use /([^\\]|^)(\w+)/g (and use the second capturing group which is the third element in the returned array).

Demonstration

Upvotes: 1

Related Questions