JavaScript 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: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)
);
Answer: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)
Answer:3

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");
Answer:4

For the first pattern you need:

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

For the second pattern:

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

Unfortunately I did not find any existing solution for my problem on stackoverflow. So, can someone help. I want to get the text of an html-element. I tried this: var element = driver.findElement(...

Unfortunately I did not find any existing solution for my problem on stackoverflow. So, can someone help. I want to get the text of an html-element. I tried this: var element = driver.findElement(...

  1. text html element
  2. text html element center
  3. html text element with id
  4. html text element color
  5. html text element attributes
  6. html text element width
  7. html text element value
  8. html text element line break
  9. html text element properties
  10. html text element tag
  11. html text element new line
  12. html text element dom
  13. html text element border
  14. html text element bold
  15. html text element font
  16. html text element input
  17. html text element code
  18. text element html div
  19. get text html element javascript
  20. set text html element

could you please tell me how to send data from one view to another view .Actually I have one input field and button on my main screen .I want to send data of input text from one screen to another ...

could you please tell me how to send data from one view to another view .Actually I have one input field and button on my main screen .I want to send data of input text from one screen to another ...

  1. send input text to javascript function
  2. send text input javascript
  3. javascript send input text value
  4. adb send text input
  5. html send text input
  6. jquery send text input
  7. input text send

I want to set an input aria-control value. But I can't do it using the traditional jQuery way. My code is this one: function showMessage() { var message = jQuery("#textToDisplay").val(); $(...

I want to set an input aria-control value. But I can't do it using the traditional jQuery way. My code is this one: function showMessage() { var message = jQuery("#textToDisplay").val(); $(...

  1. value aria-controls
  2. invalid aria attribute value aria-controls

I am creating a swipe page system using the library touchSwipe.js. With the method I have, I can swap between pages however, I cannot figure out to go back to previous page properly. Right now if I ...

I am creating a swipe page system using the library touchSwipe.js. With the method I have, I can swap between pages however, I cannot figure out to go back to previous page properly. Right now if I ...

  1. jquery swipe page
  2. jquery swipe page transition
  3. jquery swipe page example
  4. jquery swipe page change
  5. jquery page swipe effect
  6. jquery swipe div
  7. jquery swipe navigation
  8. jquery swipe div example
  9. jquery swipe div left
  10. jquery mobile swipe page example
  11. jquery mobile swipe page transition
  12. jquery swipe to next page
  13. jquery mobile swipe page navigation
  14. jquery swipe demo
  15. jquery mobile swipe page