javascript 剥离所有评论js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 剥离所有评论js相关的知识,希望对你有一定的参考价值。

function stripComments(stringIN) {
            var SLASH = '/';
            var BACK_SLASH = '\\';
            var STAR = '*';
            var DOUBLE_QUOTE = '"';
            var SINGLE_QUOTE = "'";
            var NEW_LINE = '\n';
            var CARRIAGE_RETURN = '\r';

            var string = stringIN;
            var length = string.length;
            var position = 0;
            var output = [];

            function getCurrentCharacter() {
                return string.charAt(position);
            }

            function getPreviousCharacter() {
                return string.charAt(position - 1);
            }

            function getNextCharacter() {
                return string.charAt(position + 1);
            }

            function add() {
                output.push(getCurrentCharacter());
            }

            function next() {
                position++;
            }

            function atEnd() {
                return position >= length;
            }

            function isEscaping() {
                if (getPreviousCharacter() == BACK_SLASH) {
                    var caret = position - 1;
                    var escaped = true;
                    while (caret-- > 0) {
                        if (string.charAt(caret) != BACK_SLASH) {
                            return escaped;
                        }
                        escaped = !escaped;
                    }
                    return escaped;
                }
                return false;
            }

            function processSingleQuotedString() {
                if (getCurrentCharacter() == SINGLE_QUOTE) {
                    add();
                    next();
                    while (!atEnd()) {
                        if (getCurrentCharacter() == SINGLE_QUOTE && !isEscaping()) {
                            return;
                        }
                        add();
                        next();
                    }
                }
            }

            function processDoubleQuotedString() {
                if (getCurrentCharacter() == DOUBLE_QUOTE) {
                    add();
                    next();
                    while (!atEnd()) {
                        if (getCurrentCharacter() == DOUBLE_QUOTE && !isEscaping()) {
                            return;
                        }
                        add();
                        next();
                    }
                }
            }

            function processSingleLineComment() {
                if (getCurrentCharacter() == SLASH) {
                    if (getNextCharacter() == SLASH) {
                        next();
                        while (!atEnd()) {
                            next();
                            if (getCurrentCharacter() == NEW_LINE || getCurrentCharacter() == CARRIAGE_RETURN) {
                                return;
                            }
                        }
                    }
                }
            }

            function processMultiLineComment() {
                if (getCurrentCharacter() == SLASH) {
                    if (getNextCharacter() == STAR) {
                        next();
                        next();
                        while (!atEnd()) {
                            next();
                            if (getCurrentCharacter() == STAR) {
                                if (getNextCharacter() == SLASH) {
                                    next();
                                    next();
                                    return;
                                }
                            }
                        }
                    }
                }
            }

            function processRegularExpression() {
                if (getCurrentCharacter() == SLASH) {
                    add();
                    next();
                    while (!atEnd()) {
                        if (getCurrentCharacter() == SLASH && !isEscaping()) {
                            return;
                        }
                        add();
                        next();
                    }
                }
            }

            while (!atEnd()) {
                processDoubleQuotedString();
                processSingleQuotedString();
                processSingleLineComment();
                processMultiLineComment();
                processRegularExpression();
                if (!atEnd()) {
                    add();
                    next();
                }
            }
            return output.join('');

        };

以上是关于javascript 剥离所有评论js的主要内容,如果未能解决你的问题,请参考以下文章

iOS - UIWebview - 3G 上的评论被剥离

Express采坑系列之剥离路由

评论在所有主要环境中都是 100% 安全的吗?

SQL 剥离文本并转换为整数

JavaScript 使用纯Javascript剥离HTML

JavaScript 使用javascript剥离html标签