获取字符串中最后一个' - '后面的所有字符

问题描述:

我正在一些非常严格的打包端限制中工作并且有一个客户端在他的请求中不屈不挠,所以我不得不在.js中做一些我不想做的事情。

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.

无论如何,这里有。

我有客户评论。在那些评论结束时,我有' - 美国'或' - 澳大利亚'。基本上,在每次审核结束时我都会' - [位置]'。我需要从审阅文本中提取该字符串,然后将其插入范围。我正在使用jQuery,所以我想坚持下去。

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.

我已经整理了如何浏览每个评论并将其插入我需要的地方,但我还没想出如何从每个评论中获取该文本字符串,然后从每个评论中删除它。这就是我真正可以使用一些帮助的地方。

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.

示例文本:

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

如果有帮助,我也可以使用underscore.js。

I also have underscore.js available if that helps.

实际的字符串操作不需要jQuery - 有点笨重,但很容易理解:

No need for jQuery for the actual string manipulation - a little clunky, but easy to understand:

text = 'Something -that - has- dashes - World';
parts = text.split('-');
loc = parts.pop();
new_text = parts.join('-');

所以,

loc == ' World';
new_text == 'Something -that - has- dashes ';

可以修剪或忽略空格(因为在HTML中通常无关紧要)。

Whitespace can be trimmed or ignored (as it often doesn't matter inside HTML).