1

I want to remove << any words #_ from the following string.

stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ ";

Result mast be:

Start words I love kind <<man>>, john <<kind man>> is really <<great>> end words

I tried like this:

stringVal = stringVal.replace(/^.*<<.+\#_.*$/g, "");

But it removes all string.

Note: << any words #_ may exists multiple time in string, at the start, in the middle or at the end

7
  • regex101.com/r/Dz75Hj/1 Commented May 4, 2020 at 13:41
  • 1
    @Andreas, thank you for comment, I checked it was same as mine Commented May 4, 2020 at 13:44
  • Isn't it <<john#_ instead of <<john#? Commented May 4, 2020 at 13:44
  • 1
    I added the comment because of the explanation of your regular expression which should make it clear why it removes everything Commented May 4, 2020 at 13:45
  • 1
    Inferring from your example you might be looking for <<(\w+)# which needs to be replaced by $1, see a demo on regex101.com. Commented May 4, 2020 at 13:48

2 Answers 2

3

Inferring from your examples, you might be looking for:

stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ ";

stringVal = stringVal.replace(/<<([-\w ]+)#_/g, "$1");
console.log(stringVal);

To allow other characters, change the \w+ to e.g. [-\w ]+.
See an additional demo on regex101.com.

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

3 Comments

@Muhammad: No, then you need to change the expression to [-\w ]+ as stated in the answer in the end.
thank you and now only does not work at the start and at the end: like -> <<#hello_ I <<lo and ve#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<#he llo
@Muhammad: Please edit your question and put all your required conditions in there.
2

Instead of using .+\#_, and you want to match any words you could match word characters optionally repeated by matching a space space and word characters.

<<(\w+(?: \w+)*)#_

Regex demo

In the replacement use group 1 $1

Note that you don't have to escape #

const regex = /<<(\w+(?: \w+)*)#_/g;
stringVal = "I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>>";
const result = stringVal.replace(regex, '$1');

console.log(result);

3 Comments

Thank you very much, your answers works well, and also this works well <<(\w+)#_ from @Anreas
@Jan, is right if there is space does not work by the I up voted for the efforts thank you
@Muhammad If there can be any words as in plural, this could be another option <<(\w+(?: \w+)*)#_ regex101.com/r/kh5ktF/1 If there can not be underscores in the word, only after the # then this might also be an option <<([^\W_]+(?: [^\W_]+)*)#_ regex101.com/r/eoyrYB/1

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.