Regex replace enters between quotes

前端 未结 2 690
天涯浪人
天涯浪人 2020-12-31 14:07

How do i replace all enters between two quotes in a text file. The first quote is always preceded by a tab or it is the first character in the row (csv file). I tried the fo

相关标签:
2条回答
  • 2020-12-31 14:53
    \n(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)
    

    You can use this and replace by empty string.

    See Demo

    var re = /\n(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)/g; 
    var str = 'xx "xx \nxx \nxx" \nxx \n"xx"\nxx \nxx\n"xxx xxx \nxx"';
    var subst = ''; 
    
    var result = str.replace(re, subst);
    
    0 讨论(0)
  • 2020-12-31 15:11

    With Javascript replace you can use a function as replacement.

    var str = 'foo \n"a\n" bar\n';
    
    str = str.replace(/"[^"]+"/g, function(m) {
     return m.replace(/\n/g, ' ');
    });
    
    console.log(str);

    The regex "[^"]+" will match quoted stuff with one or more non-quotes in between.

    Add conditions such as tab or start to the pattern as needed: (?:\t|^)"[^"]+"

    0 讨论(0)
提交回复
热议问题