0

I have a string pattern like following.

sales_order/view/order_id/155/key/0ee9098a18ccbca0879bfa93355fa1c7/

I want to get the number between "order_id" and "key" text this number can be of any length. How can I find the number between these two words?

This number '155' is dynamic.

Mukesh
  • 7,630
  • 21
  • 105
  • 159

4 Answers4

5

Using a regex:

var num = +( /order_id\/(\d+)\//.exec( str ) || '' )[ 1 ]

The || '' piece of that is in case there is no match. Also, if you specifically want to check for key, just add that to the end (between the two //'s at the end there)

2

Try with split this

var url = "sales_order/view/order_id/155/key/0ee9098a18ccbca0879bfa93355fa1c7/"
var stuff = url.split('/');
var order_id = stuff['3'];    //since segment 4 will be the order id
alert("Order id is "+ order_id);

You can also get the individual url segments with this method....

GautamD31
  • 28,552
  • 10
  • 64
  • 85
1

try http://jsfiddle.net/hSHkE/

 <div class="test">sales_order/view/order_id/155/key/0ee9098a18ccbca0879bfa93355fa1c7/</div>


var test=  $('.test').html().split('/')[3];
    alert(test);
ShibinRagh
  • 6,530
  • 4
  • 35
  • 57
1

Here you go:

var str="sales_order/view/order_id/155/key/0ee9098a18ccbca0879bfa93355fa1c7/";
var n=str.indexOf("order_id/");
var m=str.indexOf("/key");

alert(str.substring(n + ("order_id/").length,m));

Live Demo: JSFiddle

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105