0

I have a string which contains some pattern of text, and I need to find that pattern.

For Example:

var texttochange = ' [variable field="##Menu.name##" /]<br>                     [img value='##Menu.image##' width='200' height='200'                     borderthickness='0' keepaspect='0' bordercolor='#'                     alttext='' borderstyle='solid'][/img]<br>                     [variable field="##Menu.description##" /]<br>                     [variable field="##Menu.cost##" /]<br>';

And I wrote the below regex to find this pattern##sometext.sometext##

var regexp = /##\S+.\S+##/gi;var matchesfound = regexp.exec(texttochange);

According to my requirement I need all the fields ie["##Menu.name##"],["##Menu.image##"],["##Menu.description##"],["##Menu.cost##"]But in thevar matchesfound, I am only getting["##Menu.name##"]

akki's user avatar
akki
2,3422 gold badges26 silver badges39 bronze badges
askedApr 21, 2014 at 9:11
Furquan Khan's user avatar

2 Answers2

1

Try this:

var str = ' [variable field="##Menu.name##" /]<br>            [img value="##Menu.image##" width="200" height="200"             borderthickness="0" keepaspect="0" bordercolor="#"             alttext="" borderstyle="solid"][/img]<br>            [variable field="##Menu.description##" /]<br>            [variable field="##Menu.cost##" /]<br>';var regex = /##\S+.\S+##/gi, result, indices = [];while ( (result = regex.exec(str)) ) {  indices.push(result.toString());}console.log(indices);

Working Demo

answeredApr 21, 2014 at 9:19
Milind Anantwar's user avatar
Sign up to request clarification or add additional context in comments.

2 Comments

+1 It worked, but you are pushing result.index in the indices, whereas i need the value... Anyways.. Thankz
@FurquanKhan: useresult/result.toString()
0
var texttochange = '[variable field="##Menu.name##" /]<br>[img value="##Menu.image##" width="200" height="200" borderthickness="0" keepaspect="0" bordercolor="#" alttext="" borderstyle="solid"][/img]<br>[variable field="##Menu.description##" /]<br>[variable field="##Menu.cost##" /]<br>'var regexp = /##\S+.\S+##/gi;texttochange.match(regexp)["##Menu.name##", "##Menu.image##", "##Menu.description##", "##Menu.cost##"]
answeredApr 21, 2014 at 9:24
Anoop's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.