I am attempting to write a function that takes an array of characters that make up a string (including the start and end '"') and returns the string that makes up the array.

我正在尝试编写一个函数,它接受构成字符串的字符数组(包括开头和结尾'“')并返回组成数组的字符串。

example input / output:

示例输入/输出:

input = ['"', 'h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e', '"'];
output = "hello there";

or:

EDIT (clarify invalid array example):

编辑(澄清无效的数组示例):

var str = '"\\\\\\"\\"x\\""'
JSON.parse(str);  // returns "\\"\"x\""
// I want my function to work the same way but after converting the str to an array
var array = str.split("");

Right now I have the following:

现在我有以下内容:

var makeString = function(array){
    var result = "";
    var arr = string.split('');
    var runner = true;
    var i = 1;
    while (arr[i]){
       // This if statement doesn't work, but it is intended to 
       // account for any double quotes inside the string
        if (arr[i] === '"' && arr[i-1] !== '\\'){
            return result;
        }
        result += arr[i];
        i++;
    }
};

my function doesn't really work, but I also need it to account for all uses of escape characters and \r \n \t etc (which I don't really understand in the first place).

我的功能并没有真正起作用,但我还需要它来解释转义字符和\ r \ n \ t等的所有用法(我一开始并不是真的理解)。

EDIT / addition: from https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js I am trying to create something like crockford did to parse a string with the sub function, except I want to take the current state of the input and convert it to an array and parse it through the array elements.

编辑/补充:来自https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js我试图创建类似crockford的东西,用子函数解析一个字符串,除了我想要输入的当前状态并将其转换为数组并通过数组元素进行解析。

    string = function () {

// Parse a string value.

        var hex,
            i,
            string = '',
            uffff;

// When parsing for string values, we must look for " and \ characters.

        if (ch === '"') {
            while (next()) {
                if (ch === '"') {
                    next();
                    return string;
                }
                if (ch === '\\') {
                    next();
                    if (ch === 'u') {
                        uffff = 0;
                        for (i = 0; i < 4; i += 1) {
                            hex = parseInt(next(), 16);
                            if (!isFinite(hex)) {
                                break;
                            }
                            uffff = uffff * 16 + hex;
                        }
                        string += String.fromCharCode(uffff);
                    } else if (typeof escapee[ch] === 'string') {
                        string += escapee[ch];
                    } else {
                        break;
                    }
                } else {
                    string += ch;
                }
            }
        }
        error("Bad string");
    },

1 个解决方案

#1


0

To strip out the escape characters, you need to use the string's replace function and take in a regex as a parameter

要删除转义字符,您需要使用字符串的替换函数并将正则表达式作为参数

function arrayToString(array) {
    return array.join('').replace(/[\\\"]/g, "")
}

Test Case

var input = ['"', 'h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e', '"'];
arrayToString(input)
=> "hello there"

更多相关文章

  1. 推json敲出可观察的数组
  2. 11、javascript中字符串常用操作总结、JS字符串操作大全
  3. javascript如何取字符串中的数字
  4. 如何获取knockoutjs可观察数组的下一个元素?
  5. 显示json数组中的所有项目
  6. 图表。js数据数组使用PHP, MySQL。如何从JSON数组定义数据源?
  7. 将JavaScript对象转换为要插入关系数据库的数组
  8. javascript数组和对象是否有设置顺序?
  9. 从另一个数组中删除数组的内容。

随机推荐

  1. [置顶] Android 5.1 open data flow 数据
  2. Android 3.1 r1 中文API文档 (120) ——
  3. Android客户端与服务器用Socket进行通信
  4. 【专题】Android 启动流程相关
  5. Android 安卓让LinearLayout放置于底部的
  6. Android UI 优化-使用theme 预加载
  7. android textview 实现跑马灯效果
  8. SQLite语法与Android数据库操作
  9. setEmptyView
  10. Android UI(1)Getting Started - First A