Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/  
like image 291
Ankpro Twentyfive Avatar asked Dec 24 '12 19:12

Ankpro Twentyfive


People also ask

How do you split a string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How use split method in jQuery?

var data = $('#date'). text(); var arr = data. split('/'); $("#date"). html("<span>"+arr[0] + "</span></br>" + arr[1]+"/"+arr[2]);

What does the explode () function do?

The explode function is utilized to "Split a string into pieces of elements to form an array". The explode function in PHP enables us to break a string into smaller content with a break. This break is known as the delimiter.

What is explode in JavaScript?

If you want to explode or split a string from a certain character or separator you can use the JavaScript split() method. The following example will show you how to split a string at each blank space. The returned value will be an array, containing the splitted values.


2 Answers

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('|');
like image 68
Gabriele Petrioli Avatar answered Sep 22 '22 12:09

Gabriele Petrioli


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()

like image 22
Sushanth -- Avatar answered Sep 21 '22 12:09

Sushanth --