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.

sai
  • 487
  • 1
  • 7
  • 16
  • 1
    The `i` modifier on regexes makes the match case-independent. It looks to me like that might simplify your problem. – O. Jones Feb 15 '21 at 13:48

2 Answers2

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 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 – sai Feb 16 '21 at 06:54
  • 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 Feb 18 '21 at 12:55
  • 1
    @sai My current code yields a different result: `Hi .{0,5} Thank you .{0,5}.{0,5} for visting us` – Wiktor Stribiżew Feb 18 '21 at 13:01
  • Sorry I have misunderstood, it works, but again i have a doubt, if we want to mention 3 conditions in separators_regex, can we mention and can we use x,y,z with the ternary condition. Like var separators_regex = /{#val#}|([!-\/:-@[-`{-~])|([\s])/gi; .. Please clarify me – sai Feb 19 '21 at 03:34
  • 1
    @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. – Wiktor Stribiżew Feb 19 '21 at 10:19
  • var separators_regex = /{#val#}|([\s?])|([!-\/:-@[-`{-~])/gi; sentence1 = sentence1.replace(separators_regex, (x,y,z) => y ? "" : z ? "\s?" : ".{0.5}"); , I dont want to replace /s? in the sentence, Please help me where Im going wrong? – sai Feb 19 '21 at 10:40
  • 1
    @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}");` – Wiktor Stribiżew Feb 19 '21 at 10:50
  • Thank you so so much – sai Feb 19 '21 at 11:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228953/discussion-between-sai-and-wiktor-stribizew). – sai Feb 19 '21 at 14:50
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

  • 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 – sai Feb 16 '21 at 06:54
  • 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 – sai Feb 18 '21 at 12:37