0

I have string of the following format

Pending status started at 11/03/2019 11:32
User: XY_Z
moj/f112
Reason: Linked to Major/P1 Ticket

In the above scenario I want to remove the forward slash (/) in moj/f112 and Major/P1 but not in the date i.e 11/03/2019.

I tried \D\/\D/ig but then it will select all the forward slashes and did some trials like ^(\d{2}\/\d{2}\/\d{4})(and ?)\D\/\D/ig . I am not used to working with regular expressions, and running short of time.

Any help ?

Thanks a lot in advance. :)

Salim Shamim
  • 436
  • 5
  • 17

3 Answers3

1

You may use this alternation regex to first match and group what you want to keep and then match / as last option in alternation:

var str = `Pending status started at 11/03/2019 11:32
User: XY_Z
moj/f112
Reason: Linked to Major/P1 Ticket`

var re = /(\b(?:\d{1,2}\/){2}\d{4}\b)|\//

var repl = str.replace(re, '$1')

console.log(repl)

RegEx Demo

anubhava
  • 664,788
  • 59
  • 469
  • 547
  • 1
    This is really a smart solution (as it doesn't depend upon lookbehinds which isn't available in all JavaScript environment) where as other solutions are either wrong or partially working and still they received upvote where as this one which indeed deserved upvotes, hasn't received any. Not sure, what's going on in top regexer's minds. Accept my upvote and I wish I could do even more for such smart answers. – Pushpesh Kumar Rajwanshi Mar 13 '19 at 05:58
  • 1
    @PushpeshKumarRajwanshi: Thanks for kind words. I was also puzzled why would someone look of fancy and non-compliant solutions when there is a simple alternative available. Moreover look behind regex doesn't eve work for many cases where `/` is not within date string such as `abc/$ 1A/2 12/15 10/xy` – anubhava Mar 13 '19 at 06:03
  • 1
    Exactly, and the only reason I can think of is, probably they could not understand it, although it is simple and precise solution and indeed wise solution. May be they should have spent sometime getting your solution. – Pushpesh Kumar Rajwanshi Mar 13 '19 at 06:09
1

This regex will match {slash + two digits + slash} and rest of the slashes and then we can replace rest of the slashes.

const str = `Pending status started at 11/03/2019 11:32
User: XY_Z
moj/f112
Reason: Linked to Major/P1 Ticket`;

const regex = /(\/\d{2}\/)|\//g;
let modifiedStr = str.replace(regex, '$1');
console.log(modifiedStr)

Working example

Pal Singh
  • 1,913
  • 1
  • 14
  • 28
  • 1
    @SalimShamim It will work in Chrome, and other JS environments that support ECMAScript 2018 standard. It won't work in Firefox and IE. – Wiktor Stribiżew Mar 12 '19 at 08:15
  • 2
    Apart from non-compliance issue, it won't remove `/` in many cases where `/` is not within date string such as `abc/$ 1A/2 12/15 10/xy` – anubhava Mar 13 '19 at 06:12
  • Not working in node- `Invalid regular expression: /(?<=[a-zA-Z])(\/)(?=[a-zA-Z])/: Invalid group` – Salim Shamim Mar 15 '19 at 07:19
0

Here is another option.

var str = `Pending status started at 11/03/2019 11:32
User: XY_Z
moj/f112
Reason: Linked to Major/P1 Ticket`;

var newString = str.replace(/[^\d{1,2}\/\d{1,2}\/\d{4}]\//gm, '');

console.log(newString);