3
我想將字符串拆分成固定數量的字符串,但計數應該從後向前。在下面的例子中,我想字符串以組3的炭分成數組:從後向拆分字符串
InputString = '1234567'
尋求:
OutputArray= [1,234,567]
嘗試:
InputString.match(/.{1,3}/g)
OutputArray = [123,456,7]
我想將字符串拆分成固定數量的字符串,但計數應該從後向前。在下面的例子中,我想字符串以組3的炭分成數組:從後向拆分字符串
InputString = '1234567'
尋求:
OutputArray= [1,234,567]
嘗試:
InputString.match(/.{1,3}/g)
OutputArray = [123,456,7]
使用String#match
與positive lookahead assertion正則表達式。
var InputString = '1234567';
console.log(
InputString.match(/\d{1,3}(?=(\d{3})*$)/g)
)
隨着String#split
方法與positive lookahead assertion用於斷言位置分裂。
var InputString = '1234567';
console.log(
InputString.split(/(?=(?:\d{3})+$)/)
)
該訣竅感謝。 –
@BhagwanThapa:很高興幫助:) –