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>
1
  • ref = String(ref).substr(ref.indexOf('profile')); should be ref = String(ref).substr(ref.indexOf('profile') + 8); as profile/ is 8 characters long. Commented Nov 8, 2012 at 12:00

4 Answers 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

Sign up to request clarification or add additional context in comments.

Comments

0

Just change indexof to LastIndexOf

ref = String(ref).substr(ref.lastIndexOf('/')+1);

Comments

0

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/

Comments

0

if you want to resolve this task by regexp, use:

 ref = ref.replace(/^.*\/(\d+)$/g, "$1")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.