Linkz456
Linkz456

Reputation: 21

Alternative to Positive Lookbehind

I was trying to parse certain parts of a C code using regular expressions, for example:

    }StructureTypeDef;
         AnotherType    HwChannelType;

The thing I want to match is "StructureTypeDef" after the "}" symbol (but not including it), and I know I can achieve this using

(?<=\})\s*\w+

But the issue is that js does not support this last statement. Can anybody help?

Edit: Thanks all for your answers but I should have told that I need to achieve this just by using a regex, since the parsing doesn't allow extracting strings, so I need an equivalence of the regex shown above I have tried many ways but without success.

Upvotes: 2

Views: 1643

Answers (3)

spencer.sm
spencer.sm

Reputation: 20528

I would use the following pattern:

}\s*(\w+)\s*;

This gives you a capture group of one or more 'word' characters inside } and ;

Index 1 will give you "StructureTypeDef".
Index 0 will give you the full captured string.

text.match(/}\s*(\w+)\s*;/)[1]

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115212

You can use capturing group in regex and get the captured value.

var str = `}StructureTypeDef;
         AnotherType    HwChannelType;`;

console.log(str.match(/\}\s*(\w+)/)[1])

Upvotes: 3

ndnenkov
ndnenkov

Reputation: 36101

You can put it in a group (using ()) and fetch only the group's contents (in this case - the first group - [1]):

code.match(/}\s*(\w+)/)[1] // => "StructureTypeDef" 

Upvotes: 1

Related Questions