0

The string that i have is

[<span class="link" nr="28202" onclick="javascript:openAnlaggning('/kund/Bridge/Main.nsf/AnlOkatSamtligaFaltCopy/045C9DDFDC7AB308C1257C1B002E11F1?OpenDocument&urval=1');" >Alingsås Järnvägsstation</span>]

The logic is to check if there is a '[' at the start of the string and if it is present then take the value between the square brackets. In the above string what i would like to get as output is

<span class="link" nr="28202" onclick="javascript:openAnlaggning('/kund/Bridge/Main.nsf/AnlOkatSamtligaFaltCopy/045C9DDFDC7AB308C1257C1B002E11F1?OpenDocument&urval=1');" >Alingsås Järnvägsstation</span>

I tried with this

var out = value.match('/\[(.*)\]/i');

enter image description here

I tried it on scriptular.com,and i do get a match.

Thanks in advance.

10
  • 2
    "doesn't work" isn't a problem explanation. Commented Jun 25, 2014 at 11:36
  • @Jack value.charAt(0) will solve further problems with IE. Commented Jun 25, 2014 at 11:40
  • @VisioN Yeah, never mind that, because substr() doesn't work in the way I had hoped =( Commented Jun 25, 2014 at 11:41
  • @Jack slice does that trick ;) Commented Jun 25, 2014 at 11:44
  • 1
    @hari your regex won't work if another ] is present, See my answer. Commented Jun 25, 2014 at 11:48

2 Answers 2

1

Remove the quotes to make the argument a real regular expression literal:

// -------------------v --------v
var out = value.match(/\[(.*)\]/i);
Sign up to request clarification or add additional context in comments.

Comments

0

You could use the below regex to get the values inside [] braces,

\[([^\]]*)\]

DEMO

Your regex \[(.*)\] will not work if there is another ] at the last. See the demo.

For this you have to make your regex to do a non-greedy match by adding ? quantifier next to *,

\[(.*?)\]

DEMO

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.