简体   繁体   中英

Javascript Regex match k that is not followed by a number 0

How can I match ever k that is not followed by a number 0 .

For example k0 is not valid while k1 is.

try this

 const regex = /k(?!0).*/g console.log('k0k1k2'.match(regex)) 

without .* to get just a element k, like this

 const regex = /k(?!0)/g console.log('k0k1k2'.match(regex)) 

You can get all 'k' and number (but not k0) and dive into all matches like this:

var s = 'k0k10k2000dasdasdasdk1k0'
var re = /k[1-9]\d*/g;
var m;
while (m = re.exec(s)) {
  console.log(m);
}

// [ 'k10', index: 2, input: 'k0k10k2000dasdasdasdk1k0' ]
// [ 'k2000', index: 5, input: 'k0k10k2000dasdasdasdk1k0' ]
// [ 'k1', index: 20, input: 'k0k10k2000dasdasdasdk1k0' ]

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