1

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 :)

3
  • i need to finde MyValueA and the value within the quotation marks and MyValueB and the value within the quotation marks Commented Aug 31, 2017 at 1:04
  • Syntax Error in your JavaScript. Fix it, then get back to us. Commented Aug 31, 2017 at 1:08
  • I think it's supposed to be plain text that he wants to run a regex on, not working javascript code. Commented Aug 31, 2017 at 1:15

2 Answers 2

1

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.

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

Comments

0

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.

Comments

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.