1

I have a sentence where in place of {#val#}, there could be any value within the limit of 5. And also I need to remove special characters except few in a sentence given below. I'm trying to do like below.

var separators = ["#val#", "#VAL#", "#vaL#", "#vAl#", "#Val#"];
let sentence = "Hi {#VAL#}, Thank you {#val#}{#val#} for &visting us!";
separators.forEach((str) => {
   regexString = regexString.replace(new RegExp(`{${str}}`, "g"), ".{0,5}"); //Replacing val with .{0,5}
});

Im trying to use ternary operator in order to only replace special characters except .{0,5}, but this seems to be not working.

regexString = regexString.replace(/([&\/\\,+()$~%'":?<>_;||`~!^@=+{}\[\]\-])/g,(x,z) => {
  x == ".{0,5}" || x == "\s" ? "" :".{0,5}"
})

Kindly help me on this.

1
  • 1
    The i modifier on regexes makes the match case-independent. It looks to me like that might simplify your problem. Commented Feb 15, 2021 at 13:48

2 Answers 2

2

Asuming that special chars you want to remove can be matched with the [!-\/:-@[-{-~]` pattern (see more variations here) you simply need

var separators_regex = /{#val#}|([!-\/:-@[-`{-~])/gi;
let sentence = "Hi {#VAL#}, Thank you {#val#}{#val#} for &visting us!";
sentence = sentence.replace(separators_regex, (x,y) => y ? "" : ".{0,5}");
console.log(sentence);

The /{#val#}|([!-\/:-@[-`{-~])/gi regex will match {#val#} in a case insensitive way, and match and capture into Group 1 any ASCII special char, will find all occurrences of the pattern inside the sentence string and will replace each match either with an empty string (if Group 1 matched) or with .{0.5} otherwise.

See the regex demo.

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

9 Comments

Thanks for your input, but what I want is to remove special characters like {,},&,! .... except .{0,5}. My output should be Hi .{0,5}, Thank you .{0,5}.{0,5} for visting us . Kindly help me to achive this
But if the sentence contains, { in between it is giving different result. For example, if i try to give let sentence = "Hi {#VAL#}, Than!k you {#val#}{#val#} for &visting us!"; which results in "Hi .{0,5}, Than.{0,5}k you {0,5}{0,5} for visting us". Please help.
@sai My current code yields a different result: Hi .{0,5} Thank you .{0,5}.{0,5} for visting us
@sai You may add as many conditions as you can. In (x, y, z) => ..., x is the whole match and the rest are capturing group values in the order of appearance inside the regex pattern.
@sai In the /{#val#}|([\s?])|([!-\/:-@[-`{-~])/gi regex, Group 1 matches a ? or whitespace that you want to preserve, so, it is y in the callback arguments. Use .replace(separators_regex, (x,y,z) => y ? y : z ? "" : ".{0.5}");
|
1

As I understood it:

const separators = ["#val#", "#VAL#", "#vaL#", "#vAl#", "#Val#"];
const sentence = "Hi {#VAL#}, Thank you {#val#}{#val#} for &visting us!";

const replace = (string) => {
  const staticRegex = new RegExp(/[^a-zA-Z {.}]+(?![^{]*})/, "gi")
  separators.forEach((str) => {
    const dynamicRegex = new RegExp(`{${str}}`, "gi");
    string = string.replace(dynamicRegex, ".{0,5}");
  });
  return string.replace(staticRegex, "");
};

console.log(replace(sentence)); /* output :
Hi .{0,5} Thank you .{0,5}.{0,5} for visting us
*/

Added the i (case-insensitive) modifier as suggested by O. Jones.
Probably not the most 'Big O efficient' way, but I hope it answers your question.

/e: edited after your comment

2 Comments

Thanks for your input, but what I want is to remove special characters like {,},&,! .... except .{0,5}. My output should be Hi .{0,5}, Thank you .{0,5}.{0,5} for visting us . Kindly help me to achive this
But if we include { in the middle of sentence some where, it is not replaced. For example, const sentence = "Hi {#VAL#}, Thank y{ou {#val#}{#val#} for &visting us!" which returns Hi .{0,5} Thank y{ou .{0,5}.{0,5} for visting us

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.