Getting all characters after the last '-' in a string

I work within the framework of some very strict package restrictions and for a client who is inexorably in his request, so I have to do something in .js that I would not do.

Anyway, here it goes.

I have customer reviews. At the end of these reviews, I have “United States” or “Australia”. Basically, at the end of each review, I have "- [location]". I need to pull this line from the review text and then insert it into the range. I use jQuery, so I would like to stick with this.

I figured out how to perform each review and paste it where I need it, but I did not understand how to get this line of text from each review, and then remove it from each review. This is where I really could help.

Sample 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 if that helps.

+6
source share
6 answers

No jQuery needed for actual string manipulation - a bit awkward, 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 '; 

Spaces can be trimmed or ignored (since this often does not matter inside HTML).

+14
source

First split into a “-” to give you an array of lines between the dashes. Then use it as a stack and pull out the last item and draw a crop to remove all those annoying spaces (unless you like your spaces, of course).

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

So using jQuery it will 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" 
+9
source

The simplest way is probably using jQuery to get the element and native JavaScript to get the string:

 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 understand. Some things to look for during training: jQuery each

+3
source

Try something like this

 str2 = str.substring(str.lastIndexOf("-")) 
+2
source
 var val = $('.v2_review-text').text(); var city_array = val.split('-'); var city = city_array[city_array.length - 1]; 

Hope I helped you buddy.

+2
source
 var completeText = $('.v2_review-text')[0].value; var country = completeText.substr(completeText.lastIndexOf('-'), completeText.lenght - 1); 
+1
source

Source: https://habr.com/ru/post/953275/


All Articles