Tba3bi3 Tba3bi3 - 2 years ago 57
Javascript Question

conditionally delete numbers from string

I have two types of Strings like the following :

78:24207 PERF keytouchListAction ProtocolSelect 04.00 : 1502876877199 1502876879180


or

78:27247 PERF create tab state : 1502876879180


I'm looking for a regex to delete the numbers at the start of the string
78:24207


to produice something like this :

PERF keytouchListAction ProtocolSelect 04.00 : 1502876877199 1502876879180


or

PERF create tab state : 1502876879180


and then if the string containt two numbers after the
:
take only the first number :

PERF keytouchListAction ProtocolSelect 04.00 : 1502876877199 1502876879180


becomes :

PERF keytouchListAction ProtocolSelect 04.00 : 1502876877199


I've tried this
replace(/^\d+\.\s*/, '');
for the first pattern but does't seem to work.

and this pattern for the second problem
replace(\:.*$)
but nothing changed in my string.

Any hints on what I'm doing wrong ?

Answer Source

As an alternative: don't replace - get the wanted content with match using

[a-z].*:\s*\d+

It matches a letter and everything after it up to a colon, followed by (optional space and) a number.

document.write(
    '78:24207 PERF keytouchListAction ProtocolSelect 04.00 : 1502876877199 1502876879180'
    .match(/[a-z].*:\s*\d+/i)
);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download