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.
Use the . substring() method to get the access the string after last slash.
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.
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.
“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.
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).
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With