Reputation: 571
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/
Upvotes: 56
Views: 299025
Reputation: 48693
You can create a couple jQuery plugin functions to perform this task.
I added a chaining example to show how easy you can transform a string between subsequent calls. This adds a little overhead, but showcases how to perform function chaining for better readability.
(function($) {
$.sliceAfterIndex = function(str, index) {
return str.slice(index);
};
$.sliceAfter = function(str, substr) {
return $.sliceAfterIndex(str, str.indexOf(substr) + substr.length);
};
$.tokenAt = function(str, delimiter, index) {
return str.split(delimiter)[index];
};
$.strChain = function(str) {
return {
__str: str,
sliceAfterIndex: function(index) {
this.__str = $.sliceAfterIndex(this.__str, index); return this;
},
sliceAfter: function(substr) {
this.__str = $.sliceAfter(this.__str, substr); return this;
},
tokenAt: function(delimiter, index) {
this.__str = $.tokenAt(this.__str, delimiter, index); return this;
},
value: function(delimiter, index) {
return this.__str;
}
};
};
})(jQuery);
var res = 'row=Shimla|1|http://vinspro.org/travel/ind/';
// Nesting (inside-out)
console.log($.tokenAt($.sliceAfter(res, 'row='), '|', 2));
// Chaining (ltr)
console.log($.strChain(res).sliceAfter('row=').tokenAt('|', 2).value());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Upvotes: 0
Reputation: 55750
Split method an array . You can access the individual values by using an 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()
Upvotes: 11
Reputation: 483
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];
Upvotes: 2
Reputation: 1124
Try This
var data = 'allow~5';
var result=data.split('~');
RESULT
alert(result[0]);
Upvotes: 1
Reputation:
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);
Upvotes: 8
Reputation: 14373
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.]
Upvotes: 4
Reputation: 196217
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.
row
is a string then row.split('|');
$(row).text().split('|');
input
element then $(row).val().split('|');
Upvotes: 141