替换 JavaScript 正则表达式中的空格和换行。

huangapple go评论82阅读模式
英文:

javascript replace regex spaces and newlines

问题

I want

function main(){var a='hello';console.log(a);}
英文:

how to replace spaces and newlines together with additional closing code ; in javascript?

function main(){
   var a = 'hello'
   console.log(a)
}

console.log(main.toString().replace(/[\n ]/g,''))

output

functionmain(){vara='hello'console.log(a)}

I want

function main(){var a='hello';console.log(a);}

答案1

得分: 1

以下是翻译好的内容:

也许这是正确的

function removeSpaces(str){
        str = str.replace(/[\n]/g,';')
    let res = str.replace(/[\n ;]/g,(e, i)=>{
       switch(e){
         case ';':
           if(!'{:['.includes(str.substr(0, i).slice(-1))){
             return ';'
           }
        default:
          let arr = str.substr(0, i).split(' ')
          let lastArr = arr[arr.length - 1]
          // 需要空格的代码
          if(['function','var','let','const'].includes(lastArr)){
             return ' '
          }
          return ''
        }
     })
    return res;
}
function main(){
   var a = 'hello'
   console.log(a)
}

let str = main.toString()
console.log(str.replace(/[\n ]/g,''))
let res = removeSpaces(str)
console.log(res) // function main(){var a='hello';console.log(a);}
英文:

maybe this is correct
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function removeSpaces(str){
        str = str.replace(/[\n]/g,&#39;;&#39;)
    let res = str.replace(/[\n ;]/g,(e, i)=&gt;{
       switch(e){
         case &#39;;&#39;:
           if(!&#39;{:[&#39;.includes(str.substr(0, i).slice(-1))){
             return &#39;;&#39;
           }
        default:
          let arr = str.substr(0, i).split(&#39; &#39;)
          let lastArr = arr[arr.length - 1]
          // need space code
          if([&#39;function&#39;,&#39;var&#39;,&#39;let&#39;,&#39;const&#39;].includes(lastArr)){
             return &#39; &#39;
          }
          return &#39;&#39;
        }
     })
    return res;
}
function main(){
   var a = &#39;hello&#39;
   console.log(a)
}

let str = main.toString()
console.log(str.replace(/[\n ]/g,&#39;&#39;))
let res = removeSpaces(str)
console.log(res) // function main(){var a=&#39;hello&#39;;console.log(a);}

<!-- end snippet -->

答案2

得分: 1

Based on your requirements, here is the translated content:

基于您的要求,我认为这个正则表达式可以完成任务:

1- 第一部分:

(?&lt;![\{\}])[\n] -&gt; 匹配没有分号的语句结束;

2- 第二部分:

([\{\}\;])(\n) -&gt; 匹配{、}或;之后的换行符;

3- 第三部分:

\b\s\B|\B\s\b|\B\s\B -&gt; 匹配空白字符
英文:

Based on your requirements, I think this regex can do the job:

/(?:(?&lt;![\{\}])[\n]|([\{\}\;])(\n)|\b\s\B|\B\s\b|\B\s\B)/g

1- part one :

 (?&lt;![\{\}])[\n] -&gt; matches ending of statements without ;

2- part two:

 ([\{\}\;])(\n) -&gt; matches newlines after { or } or ;

3- part three:

 \b\s\B|\B\s\b|\B\s\B -&gt;match white spaces

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function main() {
    var a = &quot;hello&quot;
    console.log(a)
    return 1;
  }
  
const result = main.toString().replaceAll(
  /(?:(?&lt;![\{\}])[\n]|([\{\}\;])(\n)|\b\s\B|\B\s\b|\B\s\B)/g,
  function (match, p1) {
    if (match === &quot;\n&quot;) return &quot;;&quot;;
    if (match === &quot; &quot;) return &quot;&quot;;
    return p1;
  }
);

console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月20日 14:09:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76293764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定