Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  27] [ 3]  / answers: 1 / hits: 43563  / 14 Years ago, sun, october 17, 2010, 12:00:00

the string looks like this



blabla blabla-5 amount-10 blabla direction-left


How can I get the number just after amount-, and the text just after direction- ?


More From » jquery

 Answers
5

This uses regular expressions and the exec method:



var s = blabla blabla-5 amount-10 blabla direction-left;
var amount = parseInt(/amount-(d+)/.exec(s)[1], 10);
var direction = /direction-([^s]+)/.exec(s)[1];


The code will cause an error if the amount or direction is missing; if this is possible, check if the result of exec is non-null before indexing into the array that should be returned.


[#95284] Friday, October 15, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
denis

Total Points: 260
Total Questions: 87
Total Answers: 87

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;