2

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 ?

2
  • 3
    Why the downvote? OP explains the issue, and shows effort. Commented Aug 16, 2017 at 11:10
  • Use something like regex101.com for regex testing, as creating the regex ^(\d+\:\d+\s) was quite simple. --- You may not even need the capture group... Commented Aug 16, 2017 at 11:12

5 Answers 5

2

You can do this in a single replace call:

var s = '78:24207 PERF keytouchListAction ProtocolSelect 04.00 : 1502876877199 1502876879180'
var r = s.replace(/^\d+:\d+\s*|(:\s*\d+)\s+\d+$/g, '$1')

console.log(r)

s = '78:27303 PERF tab state created : 1502882663195'
var r = s.replace(/^\d+:\d+\s*|(:\s*\d+)\s+\d+$/g, '$1')

console.log(r)

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

2 Comments

this is worked for the second problem but I still get the numbers at the start of the string 78:27303 PERF tab state created : 1502882663195
78:27303 PERF tab state created : 1502882663195 is becoming PERF tab state created : 1502882663195 as you can see in my updated demo.
1

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)
);

Comments

1

You can use it by first matching and then replacing like in the code snippet

function matchAndReplace(str){
	str = str.match(/\d+:\d+ ([\w\s.]*)(:\s*\d+)/g)[0];
	console.log(str.replace(/^\d+:\d+ /,''));
}
matchAndReplace("78:24207 PERF keytouchListAction ProtocolSelect 04.00 : 1502876877199 1502876879180");
matchAndReplace("78:27247 PERF create tab state : 1502876879180");

Comments

1

For the first pattern you need:

replace(/^\d+:\d+\s+/, '');

For the second pattern:

replace(/(\s+\d+)\s+\d+$/, '$1');

Comments

1

Do you care about the pattern? If not you can just replace first 9 characters using this:

 var str = "78:27247 PERF create tab state : 1502876879180";
 var res = str.replace(/^.{9}/, "");
 console.log(res);

Comments

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.