Javascript jquery中的爆炸字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14025070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
explode string in jquery
提问by Ankpro Twentyfive
I'm getting the following result through ajax.
我通过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?
我想从它http://vinspro.org/travel/ind/。我使用了 find 和 split 功能,但它不起作用。请让我知道我怎样才能得到它?
var result=$(row).split('|');
alert(result);
chrome showing the following error
chrome 显示以下错误
Uncaught Error: Syntax error, unrecognized expression: Shimla|1|http://vinspro.org/travel/ind/
回答by Gabriele Petrioli
The splitmethodwill create an array. So you need to access the third element in your case..
该split方法将创建一个数组。所以你需要访问你的案例中的第三个元素..
(arrays are 0-indexed)You need to access result[2]to get the url
(数组是 0 索引的)您需要访问result[2]以获取 url
var result = $(row).text().split('|');
alert( result[2] );
You do not give us enough information to know what rowis, exactly.. So depending on how you acquire the variable rowyou might need to do one of the following.
您没有向我们提供足够的信息来了解究竟row是什么......因此,根据您获取变量的方式,row您可能需要执行以下操作之一。
- if
rowis a string thenrow.split('|'); - if it is a DOM element then
$(row).text().split('|'); - if it is an
inputelement then$(row).val().split('|');
- 如果
row是一个字符串那么row.split('|'); - 如果它是一个 DOM 元素,那么
$(row).text().split('|'); - 如果它是一个
input元素那么$(row).val().split('|');
回答by Sushanth --
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 elementthen you need to use $(row).val()to get the value..
如果它是输入元素,那么您需要使用它$(row).val()来获取值..
Otherwise you would need to use $(row).text()or $(row).html()
否则你需要使用$(row).text()或$(row).html()
回答by TJ-
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.
1) 我假设您在 javascript 变量“row”中捕获了 ajax 响应。如果是这样的话,这将是正确的。
var result=row.split('|');
alert(result[2]);
otherwise
除此以外
2) Use this where $(row)is a jQueryobject.
2) 使用 this where $(row)is a jQueryobject。
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.]
[正如在其他答复中提到,您可能必须使用$(row).val()或$(row).text()或$(row).html()等,这取决于什么$(行)。]
回答by TJ-
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:
split 函数用你提供的分隔符分隔文本的每一部分,你提供了“|”。所以结果将是一个包含“Shimla”、“1”和“http://vinspro.org/travel/ind/”的数组。你可以操纵它来获得第三个,“http://vinspro.org/travel/ind/”,这是一个例子:
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('|');).
如其他答案中所述,此代码会根据它是字符串 ($(str).split('|');)、文本框输入 ($(str).val().split('|') );) 或 DOM 元素($(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:
您也可以使用纯 JavaScript 获取 9 个字符后的所有内容,即“http://vinspro.org/travel/ind/”。下面是一个例子:
var str="Shimla|1|http://vinspro.org/travel/ind/";
var n=str.substr(9);
alert(n);
回答by Love Kumar
Try This
尝试这个
var data = 'allow~5';
var result=data.split('~');
RESULT
结果
alert(result[0]);

