2

I need to edit my scrip to get only ref = '521590819123';

How ?

<script type="text/javascript">
var ref = "http://www.site.ru/profile/521590819123";
if( ref.indexOf('profile') >= 0 ) {
  ref = String(ref).substr(ref.indexOf('profile'));
}
alert(ref);
 </script>
4

4 に答える 4

2

No need to use a regex :

var tokens = ref.split('/');
var whatyouwant = tokens[tokens.length-1];

Demonstration

If you really want to use a regex, you can do

var whatyouwant = /([^\/]+$)/.exec(ref)[0];

Demonstration

于 2012-11-08T12:01:00.483 に答える
0

Just change indexof to LastIndexOf

ref = String(ref).substr(ref.lastIndexOf('/')+1);
于 2012-11-08T12:01:23.540 に答える
0

使用lastIndexOf

<script type="text/javascript">
  var ref = "http://www.site.ru/profile/521590819123";
  if( ref.indexOf('profile') >= 0 ) {
    ref = ref.substr(ref.lastIndexOf('/') + 1);
  }
  alert(ref);
</script>

ここを参照してください:http://jsfiddle.net/CFeVV/

于 2012-11-08T12:02:00.570 に答える
0

このタスクを正規表現で解決したい場合は、次を使用します。

 ref = ref.replace(/^.*\/(\d+)$/g, "$1")
于 2012-11-08T12:06:42.547 に答える