简体   繁体   English

如何在没有评论的情况下从ACE编辑器中获取价值?

[英]How do I get value from ACE editor without comments?

Is it possible to get the value of an ace editor instance without the comments (single & multi row)? 是否可以在没有注释(单行和多行)的情况下获取ace编辑器实例的值? The comments are identified by the span class 'ace_comment', but the only function I found to extract the data is getValue(). 注释由span类'ace_comment'标识,但我发现提取数据的唯一函数是getValue()。

Easy Example: 简单示例:

console.log("Hello World") //This is a comment.

What I get: Output: 'console.log("Hello World") //This is a comment.' 我得到了什么:输出:'console.log(“Hello World”)//这是一个评论。

What I want: Output: 'console.log("Hello World")' 我想要的:输出:'console.log(“Hello World”)'

Extended Example (Multi-row + '//' and '/* */' comments): 扩展示例(多行+'//'和'/ * * /'注释):

*/ This is a comment */ console.log("this is not a comment") // comment again * /这是评论* / console.log(“这不是评论”)//再次评论

You can use regular expressions to remove comments: 您可以使用正则表达式删除注释:

 var string = 'console.log("Hello World") //This is a comment. \\n' + 'hello("foo"); /* This is also a comment. */'; string = string.replace(/\\s*\\/\\/.*\\n/g, '\\n').replace(/\\s*\\/\\*[\\s\\S]*?\\*\\//g, ''); document.body.innerHTML = '<pre>' + string + '</pre>'; 

A simpler solution will be. 一个更简单的解决方案。 To just trim the //Comment like this 要像这样修剪//评论

  var str = 'console.log("HelloWorld")//This is a comment'; str = str.split('//'); //Just to print out document.body.innerHTML = '<pre>You provide > '+ str[0]+'//'+str[1] +'</pre><pre>Output > ' + str[0] + '</pre>'; 

By doing this you're spliting the the entire string by the '//' split returns an array that will have [0] = console.log('HelloWorld'); 通过执行此操作,您将整个字符串拆分为'//'split,返回一个将具有[0] = console.log('HelloWorld')的数组; and the [1] = 'Comment like this'. 和[1] ='这样评论'。 This is the simplest solution i can think of. 这是我能想到的最简单的解决方案。 jcubic. jcubic。 Is basically doing the same by using a regex. 通过使用正则表达式基本上做同样的事情。 Good look! 好看!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM