0

I am trying my upmost best to get my head around regex, however not having too much luck.

I am trying to search within a string for text, I know how the string starts, and i know how the string ends, I want to return ALL the text inbetween the string including the start and end.

Start search = [{"lx":

End search = }]

i.e

[{"lx":variablehere}]

So far I have tried

/^\[\{"lx":(*?)\}\]/;

and

/(\[\{"lx":)(*)(\}\])/;

But to no real avail... can anyone assist?

Many thanks

5 Answers 5

1

You're probably making the mistake of believing the * is a wildcard. Use the period (.) instead and you'll be fine.

Also, are you sure you want to stipulate zero or more? If there must be a value, use + (one or more).

Javascript:

'[{"lx":variablehere}]'.match(/^\[\{"lx":(.+?)\}\]/);
Sign up to request clarification or add additional context in comments.

1 Comment

You might want to make + non-greedy like ioseb did
1

The * star character multiplies the preceding character. In your case there's no such character. You should either put ., which means "any character", or something more specific like \S, which means "any non whitespace character".

Comments

1

Possible solution:

var s = '[{"lx":variablehere}]';
var r = /\[\{"(.*?)":(.*?)\}\]/;
var m = s.match(r);

console.log(m);

Results to this array:

[ '[{"lx":variablehere}]',
  'lx',
  'variablehere',
  index: 0,
  input: '[{"lx":variablehere}]' ]

Comments

1
\[\{"lx"\:(.*)\}\]

This should work for you. You can reach the captured variable by \1 notation.

Comments

0

Try this:

    ^\[\{\"lx\"\:(.*)\}\]$

all text between [{"lx": and }] you will find in backreference variable (something like \$1 , depends on programming language).

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.