简体   繁体   中英

RegEx to replace spaces between numbers

I need to get rid of all the spaces between the individual time quotations (hours: minutes: seconds,miliseconds) .

This string

00: 00: 35,207 -> 00: 00: 38,664

Must become

00:00:35,207 -> 00:00:38,664

How can I do this using a RegEx ?

I tried this

 var string = "00: 00: 35,207 -> 00: 00: 38,664"; console.log(string.replace(/(?<=\\d+)\\s+(?=\\d+)/g, "")) 

But that doens't work.

Javascript does not support lookbehind (?<=

You could:

  • Select what you want to replace, in this case the colon and the whitespace :

  • Then replace that with a colon :

As @melpomene commented, you could use:

 var string = "00: 00: 35,207 -> 00: 00: 38,664"; console.log(string.replace(/: /g, ':')); 

Use the /g global flag to not return after the first match.

If you want to use the positive lookahead you could use : (?=\\d+)

 var string = "00: 00: 35,207 -> 00: 00: 38,664"; console.log(string.replace(/: (?=\\d+)/g, ':')); 

Something like this should work:

 const test = "00: 00: 35,207 -> 00: 00: 38,664"; console.log(test.replace(/\\s*(:|,)\\s*/g, "$1")); 

The regex is simply replacing : or , surrounded by whitespace with that character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM