[javascript] explode string in jquery

I'm getting the following result through ajax.

row=Shimla|1|http://vinspro.org/travel/ind/

I wanna http://vinspro.org/travel/ind/ from it. I have used find and split function but it is not working . please let me know how I can get it?

var result=$(row).split('|');
    alert(result);

chrome showing the following error

Uncaught Error: Syntax error, unrecognized expression: Shimla|1|http://vinspro.org/travel/ind/ 

This question is related to javascript jquery

The answer is


The split method will create an array. So you need to access the third element in your case..

(arrays are 0-indexed) You need to access result[2] to get the url

var result = $(row).text().split('|');
alert( result[2] );

You do not give us enough information to know what row is, exactly.. So depending on how you acquire the variable row you might need to do one of the following.

  • if row is a string then row.split('|');
  • if it is a DOM element then $(row).text().split('|');
  • if it is an input element then $(row).val().split('|');

if your input's id is following

<input type='text'  id='kg_row1' >

then you can get explode/split the above with the following function of split in jquery

  var kg_id = $(this).attr("id");
  var getvalues =kg_id.split("_");
  var id = getvalues[1];

The split function separates each part of text with the separator you provide, and you provided "|". So the result would be an array containing "Shimla", "1" and "http://vinspro.org/travel/ind/". You could manipulate that to get the third one, "http://vinspro.org/travel/ind/", and here's an example:

var str="Shimla|1|http://vinspro.org/travel/ind/";
var n = str.split('|');
alert(n[2]); 

As mentioned in other answers, this code would differ depending on if it was a string ($(str).split('|');), a textbox input ($(str).val().split('|');), or a DOM element ($(str).text().split('|');).

You could also just use plain JavaScript to get all the stuff after 9 characters, which would be "http://vinspro.org/travel/ind/". Here's an example:

var str="Shimla|1|http://vinspro.org/travel/ind/";
var n=str.substr(9);
alert(n);

Split creates an array . You can access the individual values by using a index.

var result=$(row).val().split('|')[2]
alert(result);

OR

var result=$(row).val().split('|');
alert(result[2]);

If it's input element then you need to use $(row).val() to get the value..

Otherwise you would need to use $(row).text() or $(row).html()


What is row?

Either of these could be correct.

1) I assume that you capture your ajax response in a javascript variable 'row'. If that is the case, this would hold true.

var result=row.split('|');
    alert(result[2]);

otherwise

2) Use this where $(row) is a jQuery object.

var result=$(row).val().split('|');
    alert(result[2]);

[As mentioned in the other answer, you may have to use $(row).val() or $(row).text() or $(row).html() etc. depending on what $(row) is.]


Try This

var data = 'allow~5'; 
var result=data.split('~');

RESULT

alert(result[0]);