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.

4

4 回答 4

5

使用正则表达式:

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

|| ''部分是在没有匹配的情况下。另外,如果您特别想检查key,只需将其添加到末尾(在末尾的两个 之间//

于 2013-06-17T06:35:01.267 回答
2

尝试拆分这个

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);

您还可以使用此方法获取各个 url 段....

于 2013-06-17T06:39:15.363 回答
1

试试http://jsfiddle.net/hSHkE/

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


var test=  $('.test').html().split('/')[3];
    alert(test);
于 2013-06-17T06:44:43.557 回答
1

干得好:

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));

现场演示:JSFiddle

于 2013-06-17T06:45:20.230 回答