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

Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38

5 Answers5

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".

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
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":(.+?)\}\]/);
Mitya
  • 33,629
  • 9
  • 60
  • 107
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}]' ]
ioseb
  • 16,625
  • 3
  • 33
  • 29
1
\[\{"lx"\:(.*)\}\]

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

omerkirk
  • 2,527
  • 1
  • 17
  • 9
0

Try this:

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

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

Vilius Gaidelis
  • 430
  • 5
  • 14