Reputation: 101
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>
Upvotes: 2
Views: 129
Reputation: 382474
No need to use a regex :
var tokens = ref.split('/');
var whatyouwant = tokens[tokens.length-1];
If you really want to use a regex, you can do
var whatyouwant = /([^\/]+$)/.exec(ref)[0];
Upvotes: 2
Reputation: 1254
if you want to resolve this task by regexp, use:
ref = ref.replace(/^.*\/(\d+)$/g, "$1")
Upvotes: 0
Reputation: 17545
Use 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>
See here: http://jsfiddle.net/CFeVV/
Upvotes: 0
Reputation: 12737
Just change indexof to LastIndexOf
ref = String(ref).substr(ref.lastIndexOf('/')+1);
Upvotes: 0