jQuery 获取字符串中最后一个“-”之后的所有字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18646389/
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
Getting all characters after the last '-' in a string
提问by S16
I am working within some very strict pack-end limitations and have a client that is unrelenting in his request so I'm forced to do something in .js that I'd rather not do.
我在一些非常严格的包端限制内工作,并且有一个客户对他的要求毫不留情,所以我被迫在 .js 中做一些我不想做的事情。
Anyway, here goes.
无论如何,这就去。
I have client reviews. At the end of those reviews I have '- United States' or '- Australia'. Basically, at the end of every review I have '- [location]'. I need to pull that string out of the review text and then insert it into a span. I'm using jQuery, so I'd like to stick with that.
我有客户评论。在这些评论的末尾,我有“-美国”或“-澳大利亚”。基本上,在每次评论结束时,我都有“- [位置]”。我需要将该字符串从评论文本中提取出来,然后将其插入到一个跨度中。我正在使用 jQuery,所以我想坚持下去。
I've sorted out how to run through each review and insert it where I need it, but I have not figured out how to get that string of text from each review and then remove it from each review. That's where I could really use some help.
我已经整理出如何浏览每条评论并将其插入我需要的地方,但我还没有弄清楚如何从每条评论中获取该字符串,然后从每条评论中删除它。那是我真正可以使用一些帮助的地方。
Example text:
示例文本:
<div class="v2_review-content">
<h4>These earplugs are unbelievable!</h4>
<p class="v2_review-text">These are the only earplugs I have ever used that completely block out annoying sounds. I use them at night due to the fact I am an extremely light sleeper and the slightest noise will wake me up. These actually stick to the ear in an airtight suction and do not come out at all until I pull them off in the morning. These are as close to the perfect earplug as you can get! - United States</p>
<p class="v2_review-author">Jimmy, March 06, 2013</p>
</div>
I also have underscore.js available if that helps.
如果有帮助,我也可以使用 underscore.js。
回答by Izkata
No need for jQuery for the actual string manipulation - a little clunky, but easy to understand:
不需要 jQuery 进行实际的字符串操作 - 有点笨拙,但很容易理解:
text = 'Something -that - has- dashes - World';
parts = text.split('-');
loc = parts.pop();
new_text = parts.join('-');
So,
所以,
loc == ' World';
new_text == 'Something -that - has- dashes ';
Whitespace can be trimmed or ignored (as it often doesn't matter inside HTML).
可以修剪或忽略空格(因为它在 HTML 中通常无关紧要)。
回答by Will
First split the stirng on '-' which will give you an array of strings between the dashes. Then use it as a stack and pop the last element off and call trim to remove any of that pesky whitespace (unless you like your whitespace of course).
首先在“-”上拆分搅拌,这将为您提供破折号之间的字符串数组。然后将其用作堆栈并弹出最后一个元素并调用 trim 以删除任何讨厌的空格(当然除非您喜欢您的空格)。
"String - Location".split('-').pop().trim(); // "Location"
So using jQuery it would be
所以使用jQuery它会
$('.v2_review-text').html().split('-').pop().trim(); // "United States"
Or using vanilla JS
或者使用香草JS
var text = document.getElementsByClassName('v2_review-text')[0].innerHTML;
text.split('-').pop().trim(); // "United States"
回答by fujy
Try something like this
尝试这样的事情
str2 = str.substring(str.lastIndexOf("-"))
回答by Chris
The simplest way is probably to use jQuery to get the element, and native JavaScript to get the string:
最简单的方法可能是使用 jQuery 获取元素,使用原生 JavaScript 获取字符串:
var fullReview = $('.v2_review-text').text(); //assumes only one review exists, adjust for your use.
var country = fullReview.substring(fullReview.lastIndexOf(' - ') + 1); //TODO correct for -1 if ' - ' not found.
This is just a proof of concept; the rest should be relatively easy to figure out. Some things to look up while you're learning: jQuery each
这只是一个概念证明;其余的应该相对容易弄清楚。学习时需要注意的一些事项:jQuery each
回答by Diomedes Andreou
var val = $('.v2_review-text').text();
var city_array = val.split('-');
var city = city_array[city_array.length - 1];
Hopefully i have helped you buddy.
希望我帮助了你,伙计。
回答by Julien Dumortier
var completeText = $('.v2_review-text')[0].value;
var country = completeText.substr(completeText.lastIndexOf('-'), completeText.lenght - 1);