tinfanide Posted June 29, 2012 Share Posted June 29, 2012 "[{"Title": "Book4","Barcode": "bk4","Location": "Lib4"},{"Title": "Book4","Barcode": "bk4","Location": "Lib1"}]" I want to extract{"Title": "Book4","Barcode": "bk4","Location": "Lib4"} Have tried:str.match(/[\W]*Lib4[\W]*/); but return nothing. How can I take it out with JavaScript Regular Expression? Link to comment Share on other sites More sharing options...
thescientist Posted June 29, 2012 Share Posted June 29, 2012 is there a reason you can't convert the string into JSON and then access it as an array? some browsers support it natively, if not Douglas Crockford has a library.https://github.com/douglascrockford/JSON-js var string = "[{"Title": "Book4","Barcode": "bk4","Location": "Lib4"},{"Title": "Book4","Barcode": "bk4","Location": "Lib1"}]";var json = JSON.parse(string);console.log(json[0]); Link to comment Share on other sites More sharing options...
justsomeguy Posted June 29, 2012 Share Posted June 29, 2012 Otherwise, the pattern would be to match a "{" character, then any number of characters that are not "}", then "}". 1 Link to comment Share on other sites More sharing options...
tinfanide Posted June 29, 2012 Author Share Posted June 29, 2012 Otherwise, the pattern would be to match a "{" character, then any number of characters that are not "}", then "}". Ya just inspired me. Yes, ya're very right. // IE7+ returns:// FF9 returns:found = jsonData.match(/{[^{]*Lib4[^}]*}/i); Link to comment Share on other sites More sharing options...
tinfanide Posted June 29, 2012 Author Share Posted June 29, 2012 is there a reason you can't convert the string into JSON and then access it as an array? some browsers support it natively, if not Douglas Crockford has a library.https://github.com/d...ockford/JSON-js var string = "[{"Title": "Book4","Barcode": "bk4","Location": "Lib4"},{"Title": "Book4","Barcode": "bk4","Location": "Lib1"}]";var json = JSON.parse(string);console.log(json[0]); Yes, thanks for your codes.I was doing JSON.parse but after success, I just thought that whether it would be faster if the json data is parsed into a string.As I need to loop through the json array (stored in a .JSON file) for searching purposes through AJAX, that's why I want to test if a JSON string is faster than looping through an array. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now