Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all characters after the last '-' in a string [closed]

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.

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.

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.

like image 480
S16 Avatar asked Sep 05 '13 21:09

S16


People also ask

How do you get the string after the last slash?

Use the . substring() method to get the access the string after last slash.

How do you get the string after a certain character?

To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character. Copied! We used the String.

How do you delete after last occurrence of a character?

The indexOf method returns the index of the first occurrence of a character in the string. If you need to remove everything after the last occurrence of a specific character, use the lastIndexOf method.

How do you slice a string after?

“javascript slice string after character” Code Answer'svar string = "55+5"; // Just a variable for your input. character as a delimiter. Then it gets the first element of the split string.


2 Answers

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

So,

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

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

like image 142
Izkata Avatar answered Oct 23 '22 19:10

Izkata


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

"String - Location".split('-').pop().trim(); // "Location"

So using jQuery it would be

$('.v2_review-text').html().split('-').pop().trim(); // "United States"

Or using vanilla JS

var text = document.getElementsByClassName('v2_review-text')[0].innerHTML;
text.split('-').pop().trim(); // "United States"
like image 30
Will Avatar answered Oct 23 '22 19:10

Will