Replace First N Occurrences in the String

前端 未结 6 1687
终归单人心
终归单人心 2021-01-14 06:45

How can I replace first N occurrences of many whitespaces and tabs in the following string:

07/12/2017  11:01 AM             21523 filename with         


        
6条回答
  •  情话喂你
    2021-01-14 06:53

    Derek and Nina provide great answers for dynamically replacing N whitespace groups. If N is static, the non-whitespace token (\S) can be used to match and keep the groups between whitespace:

    .replace(/\s+(\S+)\s+(\S+)\s+/, '|$1|$2|')

提交回复
热议问题