1

I'm trying to remove some part of a string in order to isolate a value of a sentence. I would like to store the thing that is liked in a var called thingLoved. So when you type "I love cats" you can have a console.log(thingLoved) = cats

In order do to that, I'm trying to remove I love from the first sentence so it only remains "apples". In that exemple I can remove love but not i love (because there is a space I guess), I don't know how to do that.

Thanks for the help.

function individuaLetters(e){
  e.each(function(){
    var letters = $(this).html().replace(/^[a-zA-Z\u00C0-\u017F]+,\s[a-zA-Z\u00C0-\u017F]+$/).toLowerCase();
    console.log(letters);
      
    if (letters.includes("i love")) {
      var thingLoved = $(this).html().replace("i love","")
      console.log(thingLoved);
    };
  })
}

var text = $('div').find('.indiv');
individuaLetters(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <span class="indiv">I love apples</span>   
</div>

4
  • 1
    It's not clear what you're asking, or what the issue is. Could you edit the question to include a better description of your goal. Commented Nov 19, 2020 at 12:02
  • its not clear what you want to do but if you want to remove space you can simply trim it using string.trim() Commented Nov 19, 2020 at 12:05
  • try replace("I love",""), where I capitalized, as in the text. Commented Nov 19, 2020 at 12:06
  • 1
    @sergeykuznetsov it's working with a capitalized I. Thanks. Commented Nov 19, 2020 at 12:09

2 Answers 2

1

Use a regular expression with the alternator (|) in case you need to have more matches and case insensitive modifier (/i):

function individuaLetters(e){
  e.each(function(){
    var letters = $(this).html().replace(/^[a-zA-Z\u00C0-\u017F]+,\s[a-zA-Z\u00C0-\u017F]+$/).toLowerCase();
    console.log(letters);
      
    if (letters.includes("i love")) {
      var reg = /i love/i
      var thingLoved = $(this).html().replace(reg,"")
      console.log(thingLoved);
    };
  })
}

var text = $('div').find('.indiv');
individuaLetters(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <span class="indiv">I love apples</span>   
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

yep, that's working and, you are right, will give more modularity. Also, since we are here, do you know if I can expand that to remove everything else just to keep apples so if I type hello, I love apples bye the output will still be apples ?
You can have a list that will act as a matcher. For eg ['apples', 'banana', 'strawberry'] you can match the input string with this list and get only apples.
yep, it brings a lot of complexity, since the person says I love bananas it won't work... Guess i'll have to find another solution to do what I want. Thanks a lot for the help!
0

A regex is probably the best way to go as per @Vishal Jain's answer, but this is a basic fix for what you were originally trying to do:

function individuaLetters(e){
  e.each(function(){
    var letters = $(this).html().replace(/^[a-zA-Z\u00C0-\u017F]+,\s[a-zA-Z\u00C0-\u017F]+$/).toLowerCase();
    console.log(letters);
      
    if (letters.includes("i love")) {
      var thingLoved = letters.replace("i love","") .trim();         
      console.log(thingLoved);     
    }
  })
}

var text = $('div').find('.indiv');
individuaLetters(text);

Bascially, you were trying to replace "i love" in the original html string, which in fact still had "I love" in it (i.e. you need to call includes() on the letters variable).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.