harikrish
harikrish

Reputation: 2019

javascript regex with long strings

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.

Upvotes: 0

Views: 705

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

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

Upvotes: 0

VisioN
VisioN

Reputation: 145378

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

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

Upvotes: 1

Related Questions