Matthias Just
Matthias Just

Reputation: 11

JS RegEx doesnt find expression

i need to find MyValueA Within the quotation marks and MyValueB Within the quotation marks. My problem that the order changes sometimes and than my regex doesnt find anything.

Someone there how know a regex to find my values? It would be also okay to run two regex for MyValueA and MyValueB.

Variante A

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ({
        StuffValue : 'Foo Blaaaa',
        MyValueA :  './selector.template.abc',  
    	MyValueB : ['./selector.styleA.gpp', './selector.styleB.gpp'] 

    }) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt

Variante B

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ({
        StuffValue : 'Foo Blaaaa',
        MyValueA 
                       :  './selector.template.abc',  
    	MyValueB : ['./selector.styleA.gpp', 
                    './selector.styleB.gpp'] 

    }) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt

Variante C

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ({
        StuffValue : 'Foo Blaaaa', MyValueA :  './selector.template.abc', MyValueB : ['./selector.styleA.gpp', './selector.styleB.gpp'] 

    }) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt

Thanks for any ideas :)

Upvotes: 1

Views: 41

Answers (2)

Duncan Thacker
Duncan Thacker

Reputation: 5188

I'd advise against using regular expressions to parse complex data structures like this - it's full of pitfalls, like escaped characters. If you really want to do it, this snippet it does it by using \s* to stand for whitespace and does "lazy" matching on the contents to avoid grabbing too much at once - (.*?).

const plaintext1 = `Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ({
        StuffValue : 'Foo Blaaaa',
        MyValueA :  './selector.template.abc',  
    	MyValueB : ['./selector.styleA.gpp', './selector.styleB.gpp'] 

    }) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt`;
    
const plaintext2 = `Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ({
        StuffValue : 'Foo Blaaaa',
        MyValueA 
                       :  './selector.template.abc',  
    	MyValueB : ['./selector.styleA.gpp', 
                    './selector.styleB.gpp'] 

    }) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt`;

const plaintext3 = `Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ({
        StuffValue : 'Foo Blaaaa', MyValueA :  './selector.template.abc', MyValueB : ['./selector.styleA.gpp', './selector.styleB.gpp'] 

    }) Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt`;

const regexA = /MyValueA\s*:\s*'(.*?)'/;
const regexB = /MyValueB\s*:\s*\[\s*'(.*?)'\s*,\s*'(.*?)'\s*\]/;

console.log( regexA.exec( plaintext1 )[1] );
console.log( regexA.exec( plaintext2 )[1] );
console.log( regexA.exec( plaintext3 )[1] );

console.log( regexB.exec( plaintext1 )[1] );
console.log( regexB.exec( plaintext1 )[2] );

console.log( regexB.exec( plaintext2 )[1] );
console.log( regexB.exec( plaintext2 )[2] );

console.log( regexB.exec( plaintext3 )[1] );
console.log( regexB.exec( plaintext3 )[2] );

You could simplify the regexes by stripping out whitespace from your text first with strippedPlaintext1 = plaintext1.replace(/\s/g, ""); but this might get rid of spaces you want.

A preferable method would be to filter out the data from the text somehow, and then use something like JSON.parse() to marshall into a javascript object.

Upvotes: 1

James
James

Reputation: 106

A regular expression to find the contents of quotations is '(((?!').)*)'. Adding a little bit to the start will let you search for MyValueA.

MyValueA\s*:\s*'(((?!').)*)'

Repeat for MyValueB and any others.

Note that you will need to extract one of the matched groups to determine the actual contents of the string.

Upvotes: 0

Related Questions