-2

I'm looking for a way to trim a specific sequence of characters from the right and left side of a string in Javascript. It would be ideal if it could be done in a case-insensitive manner.

I do not wish to use jQuery or another library (i.e., I'm hoping for a pure javascript solution, maybe regex?)

For example:

var str = "This is a test. This is a test."

// yields: " is a test. This is a test."
str.trimPhrase('this')

// yields: This is a test. This is a test
str.trimPhrase('.')

// yields: This is a test. This is a test.
str.trimPhrase('is')

// yields: just a single space (the space that was between the sentences).
str.trimPhrase('This is a test.')
Darren Gates
  • 473
  • 2
  • 18
  • [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Andreas Aug 12 '22 at 16:40
  • not a homework question – Darren Gates Aug 12 '22 at 16:43
  • But an assignment or some other learning exercise that you're supposed to solve. Same topic. – Andreas Aug 12 '22 at 16:43
  • Note: there are answers in the link above that show how to trim by multiple characters. – devlin carnate Aug 12 '22 at 16:45
  • I'm not really following you @Andreas I'm posting a question here because I legitimately am unclear on a good solution for this issue, and have not found anything on S.O. that does precisely what I need. – Darren Gates Aug 12 '22 at 16:46
  • Read the link in my comment. – Andreas Aug 12 '22 at 16:47
  • it looks like this link has a solution that fits the bill: https://stackoverflow.com/questions/26156292/trim-specific-character-from-a-string thanks, @devlincarnate – Darren Gates Aug 12 '22 at 16:49
  • "But an assignment or some other learning exercise that you're supposed to solve." This suggests that I'm a student, or posting on S.O. is a way to get out of doing some sort of educational assignment... this is not the case. I know the question probably sounds like it, but I can assure you that this is not the case @Andreas – Darren Gates Aug 12 '22 at 16:50
  • From the link: _"Search for already existing questions about your issue"_ (Devlin found a duplicate), _"Help us understand your baseline"_ (just a requirement with some test cases), _"Make a good faith attempt to solve the problem yourself first."_ (none at all), _"Ask about specific problems with your existing implementation."_ (no implementation) – Andreas Aug 12 '22 at 17:00
  • The post where this was found was titled "Trim specific character from a string", in a comment pretty far down. I would not consider this a lack of good faith effort to find an answer. While I'm grateful that an answer exists, it is often the case that the answers are embedded deep in comments. Surfacing the answer in a new post with a more accurate title might help some users. – Darren Gates Aug 12 '22 at 17:14

1 Answers1

0

This is the solution that I ended up going with, originally posted here:

Trim specific character from a string

function hasSubstringAt(str, substr, pos) {
    var idx = 0, len = substr.length;

    for (var max = str.length; idx < len; ++idx) {
        if ((pos + idx) >= max || str[pos + idx] != substr[idx])
            break;
    }

    return idx === len;
}

function trimWord(str, word) {
    var start = 0,
        end = str.length,
        len = word.length;

    while (start < end && hasSubstringAt(str, word, start))
        start += word.length;

    while (end > start && hasSubstringAt(str, word, end - len))
        end -= word.length

    return (start > 0 || end < str.length) ? str.substring(start, end) : str;
}

// Usage:
trimWord('blahrealmessageblah', 'blah');
Darren Gates
  • 473
  • 2
  • 18
  • If that's just a copy of another answer then either delete your question or close it as duplicate. – Andreas Aug 12 '22 at 16:57
  • 1
    I have noted that this is answered in another post. The solution was embedded far enough down in the comments that it wasn't at all obvious that it was an answer to the my issue. I think that the title of my question could be helpful to some users to locate this answer. – Darren Gates Aug 12 '22 at 17:02