0

I am trying to remove exclamation marks at the end of a word.

eg. remove("!!!Hi !!hi!!! !hi") === "!!!Hi !!hi !hi"

I am able to remove all exclamation marks, but am unable to target those just at the end of a word.

The following is what I currently have.

function remove(s){
  return s.replace(/([a-z]+)[!]/ig, '$1');
}
1

6 Answers 6

1

You can strip off !s that are at the end of words using the following RegExp:

"!!!Hi !!hi!!! !hi"
  .replace(/!+\s/g, ' ')  // this removes it from end of words
  .replace(/!+$/g, '')    // this removes it from the end of the last word

Result: "!!!Hi !!hi !hi"

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

Comments

1

You cant try this one:

\b!+

It matches ! that follows a word.

Comments

0

Change your regex to:

/([a-z]+)!+/ig

Then

function remove(s){
  return s.replace(/([a-z]+)!+/ig, '$1');
}

should work

Comments

0

you can use this regex

console.log("!!!Hi !!hi!!! !hi!!!".replace(/([a-z]+)[!]+/ig, '$1'))

1 Comment

This is a copy of @idmean's answer with no added value.
0

I think it will be easier not to use regex, but insted use lastIndexOf and slice

something like:

function removeQuestionmark(inputvalue) {
  var index = inputValue.lastIndexOf("!");

  if(inputValue.endsWith("!"))
    return inputvalue.slice(0,index-1);

  return `${inputvalue.slice(0,index-1)${inputValue.slice(index+1)}}
}

I have not tested the code.

Comments

0

Well you should add a + so you take more than one !

function remove(s){

    return s.replace(/([a-z]+)([!]+)/ig, "$1");
}

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.