Adding text to beginning of each array element

前端 未结 7 1524
[愿得一人]
[愿得一人] 2020-11-30 06:49

I have an array which contains the contents as follows:

[\"ZS125-48ATab\", \"STR125YBTab\", \"KS125-24Tab\", \"ZS125-50Tab\", \"DFE125-8ATab\", \"ZS125-30Tab         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 07:04

    You can do it like this :

    array = ('#' + array.join('#')).match(/#[^#]*/g) || []; // null || []
    

    The following trick works as well, but I wonder why split ignores the first sharp...

    array = ('#' + array.join('#')).split(/(?=#)/);
    

    Indeed, I rather expected this scenario : "#a#b#c" -> ["", "#a", "#b", "#c"].

    Anyway, I prefer the second method since match returns null in case of failure.

提交回复
热议问题