0

I want to replace everything other than numeric char from a string, but if + appears at the starting of the string it should be ignored. for example

1234++!@#$%^&*()_+=-;',.><:" becomes 1234
+1234 becomes +1234
++++1234 becomes +1234
+1234++!@#$%^&*()_+=-;',.><:" becomes +1234
+1234++!@#$%^&*()_+=-;',.><:" becomes +1234
+1234ABCabc++!@#$%^&*()_+=-;',.><:" becomes +1234
1234ABCabc++!@#$%^&*()_+=-;',.><:" becomes 1234
Aa1234ABCabc++!@#$%^&*()_+=-;',.><:" becomes 1234
a+1234ABCabc++!@#$%^&*()_+=-;',.><:" becomes 1234
1+1234ABCabc++!@#$%^&*()_+=-;',.><:" becomes 11234

can you please suggest?

Neeraj Pathak
  • 311
  • 2
  • 18

2 Answers2

2

You can try:

str.replace(/((?<!^)\D)|(^[^+0-9])/g, '');

This replaces (with nothing):

  • any non-digit that is not at the start of the string.
  • any non-digit except + that is at the start of the string.
Wyck
  • 10,311
  • 6
  • 39
  • 60
  • many many thanks it works :) but with a warning Unsafe Regular Expressioneslint(security/detect-unsafe-regex) but I think it's ok to disable it :( Many many thanks for the help – Neeraj Pathak Sep 12 '22 at 20:44
  • in fairness, you could have used your original strategy of `result = str.replace(/\D/g, '')` But then followed up with `if (str.startsWith('+')) result = '+' + result;` as a fixup. You were almost there, and there would be no warning. – Wyck Sep 12 '22 at 20:51
  • yeah, I already have that solution but wanted to get rid of this extra 1 line of if check that can be saved. but I think I have to go back to that solution as our old friend Safari is not able to understand this nice regex and breaks with the error => SyntaxError: Invalid regular expression: invalid group specifier name – Neeraj Pathak Sep 12 '22 at 21:12
0

This extracts numbers from a string, including an optional leading +:

var tests = [
  '1234++!@#$%^&*()_+=-;\',.><:"', // becomes 1234
  '+1234', // becomes +1234
  '++++1234', // becomes +1234
  '+1234++!@#$%^&*()_+=-;\',.><:"', // becomes +1234
  '+1234++!@#$%^&*()_+=-;\',.><:"', // becomes +1234
  '+1234ABCabc++!@#$%^&*()_+=-;\',.><:"', // becomes +1234
  '1234ABCabc++!@#$%^&*()_+=-;\',.><:"', // becomes 1234
  'Aa1234ABCabc++!@#$%^&*()_+=-;\',.><:"', // becomes 1234
  'a+1234ABCabc++!@#$%^&*()_+=-;\',.><:"', // becomes 1234
  '1+1234ABCabc++!@#$%^&*()_+=-;\',.><:"' // becomes 11234
];

tests.forEach(str => {
  console.log(str + ' => ' + str.replace(/^.*?(\+?[0-9]+).*$/, '$1'));
});

Output:

1234++!@#$%^&*()_+=-;',.><:" => 1234
VM1036:2 +1234 => +1234
VM1036:2 ++++1234 => +1234
2VM1036:2 +1234++!@#$%^&*()_+=-;',.><:" => +1234
VM1036:2 +1234ABCabc++!@#$%^&*()_+=-;',.><:" => +1234
VM1036:2 1234ABCabc++!@#$%^&*()_+=-;',.><:" => 1234
VM1036:2 Aa1234ABCabc++!@#$%^&*()_+=-;',.><:" => 1234
VM1036:2 a+1234ABCabc++!@#$%^&*()_+=-;',.><:" => +1234
VM1036:2 1+1234ABCabc++!@#$%^&*()_+=-;',.><:" => 1

Your described objective and last example contradict each other. Please be more specific what you need.

Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20
  • I don't see a contradiction. I interpret it as: Any non-digit that is not "a plus at the start of the string" should be removed. – Wyck Sep 12 '22 at 20:36