1

I have some text lines like that :

  • vt_wildshade2^508^508
  • vt_ailleurs2^1188^1188
  • ...
  • vt_high2^13652^13652

Is it possible to select with jQuery the last numbers after the second "^" and remove the other part ?

ie keeping the last number of each lines like this :

  • 508
  • 1188
  • ...
  • 13652

I know how to select class / id / specific text but i hang up here. thank in advance

1
  • i assume this is plain text within HTML tags? In that case? sounds like a job for regular expressions. Alternatively, use split('^') on the string and the last element of the array will contain your number Commented Jul 5, 2012 at 20:02

3 Answers 3

1

If the format will always be the same, you can use javascript's String.split(delimiter) method to split the string at the '^' into an array, which will return something like:

    ["vt_wildshade2", "508", "508"]

In this case, the following code would take the last bit from your line:

    var str = "vt_wildshade2^508^508";
    var numbers = str.split("^")[2];

For reference:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

If the format will not always be the same, it will be required to use regular expressions to find the data that you want in the format that you need it, as other answerers have explained.

Not sure how this data is coming in, could you clarify on how this data is displayed before the script is run?

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

Comments

1
var string = 'vt_wildshade2^508^508',
    number = string.match(/[0-9]+$/)[0];
console.log(number);

$: Matches end of string.
[0-9]: Matches numbers.
+: Matches the preceding 1 or more times.
*: Mathes the preceding 0 or more times.
You can change + to * if you are not sure if it has numbers in the end of the string, this case number will be an empty string.

Demo using +
Demo using *

Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp

6 Comments

+1 you could also add +('vt_wildshade2^508^508'.match(/[0-9]+$/)); to parse it to an number.
@lbstr To be precise, + will transform string to numeric, while match returns array on success.
but be careful with my comment above. match returns an array, so if it returns nothing you end up with +[], which returns 0. Could be undesirable...
@VisioN thank you for the correction! I caught most of that right after I posted it, but you're exactly right: it is an important distinction that its a Number, not an int.
@lbstr The problem is that if I parse as number I have to make sure the string has the number in the end, this case it'll return 0 as value.
|
0

IF you have:

vt_wildshade2^508^508

Say, from:

var theText = $('p').text();

So, equivalent to:

var theText = 'vt_wildshade2^508^508';

You can get the final "508" text with:

var finalNumber = theText.split('^')[2];

And if you need it to be a numer, something more like:

var finalNumber = (theText.split('^').length > 2) ? parseInt(theText.split('^')[2], 10) : -1;

If there is no 3rd number, the default is -1 in the code above.

2 Comments

Another question : if i want to get the number AND the name like this "wildshade : 508" how can i do that ? In fact i want to get the name with the number, then passing all in a filter function to make a rank ? anyway thank a lot !
@user1092395 I don't see where the text "wildshade" would be coming from in your question. I suggest asking a whole new question since is sounds unrelated to the data you show in the current question.

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.