Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi matches character in RegExp alway end at last one

Hello everyone, I'm a stackoverflow novice.

If I have any wrong about asking this question, hope to redress me. :)

If my test string is 'One\nTwo\n\nFour',

I use the RegExp /(.)*\n/ in JavaScript,

will match 'One\nTwo\n\n' not 'One\n', 'Two\n' and '\n' in my expectation.

And I want to get the result is 'One', 'Two', '' and 'Four'.


Very thanks @Dalorzo's answer.

'One\nTwo\n\nFour'.split(/\n/g) //outputs ["One", "Two", "", "Four"]

like image 894
Husky Avatar asked Mar 18 '23 04:03

Husky


1 Answers

Maybe better could be a split to achieve the same goal? by

With Regex:

'One\nTwo\n\nFour'.split(/\n/) //outputs ["One", "Two", "", "Four"]

or without Regex like:

'One\nTwo\n\nFour'.split('\n') //outputs ["One", "Two", "", "Four"]
like image 124
Dalorzo Avatar answered Mar 22 '23 23:03

Dalorzo