javascript用双引号逗号删除文本不起作用

时间:2020-02-12 20:44:22

标签: javascript json reactjs

我在下表中有一个json对象

<plugin name="cordova-plugin-whitelist" spec="1" />
  <access origin="*" />
  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
  <allow-intent href="tel:*" />
  <allow-intent href="sms:*" />
  <allow-intent href="mailto:*" />
  <allow-intent href="geo:*" />
  <!-- White list https access to Stripe-->
<allow-navigation href="https://*stripe.com/*"/>
<allow-navigation href="https://*js.stripe.com/*"/>
我要提取的

,需要删除一些文本。 新的json对象如下所示

 public class SentenceCapitalization
{
    public static string SentenceCapitalizations(string str)
    {
        string[] strArray = str.Split(' ');//split words
        string capString = "";
        string capStr = "";
        foreach (string s in strArray)
        {
            if (s.Length > 0 && s != string.Empty)
            {
                capStr = s[0].ToString().ToUpper();
                capString += capStr + s.Remove(0, 1) +" ";
            }
        }
        return capString.TrimEnd();
    }
}
public class Step
{
    public static void Steps(int num)
    {
        for (int row = 1; row <= num; row++)
        {
            string str = "";
            for (int col = 1; col <= num; col++)
            {
                if (row == col || row > col)
                    str += "#";
                else if (row < col)
                    str += " ";

            }
            Console.WriteLine(str);

        }

    }
    public static void RevSteps(int num)
    {
        int count = 0;
        for (int row = 1; row <= num; row++)
        {
            string str = "";
            for (int col = 1; col <= num; col++)
            {
                count = num - row;
                if(col> count)
                {
                    str += "#";
                }
                else 
                    str += " ";

            }
            Console.WriteLine(str);

        }

    }

}
public class Pyramid
{
    public static void PyramidSteps()
    {
        Console.Write("enter number for Pyramid steps: ");
        int num = (int) Math.Round(Convert.ToDecimal(Console.ReadLine()));
        int rows = num;
        int cols = num + (num - 1);
        int emptyLCols, emptyRCols;
        for (int row = 1; row <= rows; row++)
        {
            string str = "";
            emptyLCols = rows - row;
            emptyRCols = rows + row;
            for (int col = 1; col <= cols; col++)
            {
                if(col<=emptyLCols || col>=emptyRCols)
                    str += " ";                    
                else 
                    str += "#";
            }
            Console.WriteLine(str);
        }

    }

}
public class vowels
{
    public static void Vowels()
    {
        Console.Write("enter String: ");
        string vowelStr = Console.ReadLine().ToLower();
        int count = 0;


        for(int i=0; i < vowelStr.Length; i++)
        {
            char c = vowelStr[i];
            if ("aeiou".Contains(c))
            {
                count++;
            }
        }
        Console.WriteLine("total vowels...."+count);
    }

}

public class SpiralMatrix
{
    public static void SpiralMatrixs()
    {
        Console.Write("enter number for SpiralMatrixs: ");
        int num = (int)Math.Round(Convert.ToDecimal(Console.ReadLine()));
        int[,] arr = new int[num,num];

        int startColumn=0, startRow=0, endColumn=num-1, endRow=num-1;
        int counter = 1;
       for(int k=0; (startColumn < endColumn && startRow <= endRow) && k<num; k++)
        {
            for(int i= startColumn; i <= endColumn; i++)
            {
                arr[startRow, i] = counter;
                counter++;
            }
            startRow++;
            for (int i = startRow; i <= endRow; i++)
            {
                arr[i, endColumn] = counter;
                counter++;
            }
            endRow--;
            startColumn++;
            k++;
        }



    }
}

public class Sorting
{
    public static int RecFactorials(int num)
    {
        if (num == 0)
            return 1;
        else
            return num * RecFactorials(num - 1);
    }
    public static int[] BubbleSort(int[] array)
    {
        for(int partIndex=array.Length-1; partIndex > 0; partIndex--)
        {
            for(int i=0; i < partIndex; i++)
            {
                if(array[i]> array[i + 1])
                {
                    Swap(array, i, i + 1);
                }
            }                
        }
        return array;
    }
    public static int[] SelectionSort(int[] array)
    {
        for (int partIndex = array.Length - 1; partIndex > 0; partIndex--)
        {
            int largeIndexAt = 0;
            for(int i = 1; i <= partIndex; i++)
            {
                if (array[i] > array[largeIndexAt])
                {
                    largeIndexAt = i;
                }

            }
            Swap(array, largeIndexAt, partIndex);
        }
        return array;
    }
    public static int[] InsertionSort(int[] array)
    {
        for (int partIndex = 0; partIndex < array.Length ; partIndex++)
        {
            int currUnsort = array[partIndex];
            int i = 0;
            for ( i = partIndex; i >0 && array[i-1] > currUnsort; i--)
            {
                array[i] = array[i - 1];

            }
            array[i] = currUnsort;
        }
        return array;
    }
    public static void Swap(int[] arr, int i, int j)
    {
        int arrPre = arr[j];
        arr[j] = arr[i];
        arr[i] = arrPre; 
    }
}
public class Lists
{
    public static void SortList()
    {
        List<int> lsArray = new List<int>();
        for(int i=0; i < 16; i++)
        {
            lsArray.Add(i);
            LogList(lsArray);
        }
        LogList(lsArray);

        for(int i=10; i>0; i--)
        {
            lsArray.RemoveAt(i-1);
            LogList(lsArray);
        }
        lsArray.TrimExcess();
        LogList(lsArray);
        Console.ReadLine();
    }
    public static void LogList(List<int> ls)
    {
        Console.WriteLine("count: {0}, Capacity:{1}", ls.Count, ls.Capacity);
    }
    public static void ApiMembers()
    {
        var list = new List<int>() { 1, 0, 5, 3, 4 };
        list.Sort();

        int binarySearch = list.BinarySearch(5);

        list.Reverse();

        ReadOnlyCollection<int> readonlylist = list.AsReadOnly();

        readonlylist.Reverse();
        int count=list.Count();
        var listCustomers = new List<Customer>
        {
            new Customer{BirthDate=new DateTime(1988,08,12), Name="name1"},
            new Customer{BirthDate=new DateTime(1978,08,04), Name="name2"},
            new Customer{BirthDate=new DateTime(1978,09,04), Name="name3"},
            new Customer{BirthDate=new DateTime(1988,08,12), Name="name1"},
            new Customer{BirthDate=new DateTime(1998,08,12), Name="name1"},
            new Customer{BirthDate=new DateTime(1888,08,12), Name="name1"}
        };

        listCustomers.Sort((left, right) =>
        {
            if (left.BirthDate > right.BirthDate)
                return 1;
            if (right.BirthDate > left.BirthDate)
                return -1;
            return 0;
        });

    }
}
public class Customer
{
    public DateTime BirthDate;
    public string Name;
}

public class SingleLinkedList<T>
{
    public Node<T> Head { get; private set; }
    public Node<T> Tail { get; private set; }
    public int Count { get; private set; }
    private bool IsEmpty => Count == 0;
    public void AddFirst(T value)
    {
        AddFirst(new Node<T>(value));
    }

    private void AddFirst(Node<T> linkedListNode)
    {
        //save off the current head
        Node<T> tmp = Head;
        Head = linkedListNode;
        Head.Next = tmp;
        Count++;
        if(Count==1)
        { Tail = Head; }
    }

    public void RemoveFirst()
    {
        if(IsEmpty)
        {
            throw new InvalidOperationException();
        }
        Head = Head.Next;
        if (Count == 1)
            Tail = null;
        Count--;
    }
    public void RemoveLast()
    {
        if (IsEmpty)
        {
            throw new InvalidOperationException();
        }
        if(Count==1)
        {
            Head = Tail = null;
        }
        else
        {
            //find the penultimate node
            var current = Head;
            while (current.Next != Tail)
            {
                current = current.Next;

            }
            current.Next = null;
            Tail = current;
        }
        Count--;
    }
    public void AddLast(T value)
    {
        AddLast(new Node<T>(value));

    }

    private void AddLast(Node<T> node)
    {
        if (IsEmpty)
        {
            Head = node;
            Tail = node;
        }
        else
        {
            Tail.Next = node;
            Tail = node;
        }
        Tail = node;
        Count++;
    }

}
public class Node<T>
{

    public T Value { get; set; }
    public Node<T> Next { get; set; }

    public Node(T value)
    {
        this.Value = value;
    }



}
 public static void RevSteps(int num)
    {
        int count = 0;
        for (int row = 1; row <= num; row++)
        {
            string str = "";
            for (int col = 1; col <= num; col++)
            {
                count = num - row;
                if(col> count)
                {
                    str += "#";
                }
                else 
                    str += " ";

            }
            Console.WriteLine(str);

        }

    }
    public static void plusMinus()
    {
        int n = Convert.ToInt32(Console.ReadLine());

        int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp))
        ;
        plusMinus(arr);
    }
    static void plusMinus(int[] arr)
    {
        int count = arr.Length;
        decimal pC = 0, bC = 0, zC = 0;
        for (int i = 0; i < count; i++)
        {
            if (arr[i] > 0)
            {//positive
                pC++;
            }
            else if (arr[i] < 0)
            {
                bC++;
            }
            else if (arr[i] == 0)
            {
                zC++;
            }

        }
        Console.WriteLine((pC / count).ToString("N6"));
        Console.WriteLine((bC / count).ToString("N6"));
        Console.WriteLine((zC / count).ToString("N6"));

    }

一种实现方法是将对象字符串化并替换关键字, 在我的“理论”中应该起作用。但是无论如何我都无法将('{“ AND”:')替换为空白。 问题是关键字包含双引号逗号。

下面是代码: s是包含错误json的json对象。

filter: {where: { AND:[{gender: {IN: "Female"}},{age: {LTE: 44}}]}, relativeDateRange: 90}

没有双反逗号的文本将被替换。 即使使用正则表达式或转义字符也无济于事。

任何建议

3 个答案:

答案 0 :(得分:1)

您可以尝试的一种选择是使用替换功能,该功能是JSON.stringify中的参数:

// Your initial filter object
const filter = {filter: {where: { AND:[{gender: {IN: "Female"}},{age: {LTE: 44}}]}, relativeDateRange: 90}};

// Define your replacer function:
const replacer = (name, value) => {
    if (name === 'where') {
        const array = value['AND'];
        // Merge array elements or alter your output in any other way
        return Object.assign({}, array[0], array[1]);
    }
    return value;
}

// The resulting output
console.log(JSON.stringify(filter, replacer, 2));

将产生以下输出:

{
  "filter": {
    "where": {
      "gender": {
        "IN": "Female"
      },
      "age": {
        "LTE": 44
      }
    },
    "relativeDateRange": 90
  }
}

答案 1 :(得分:0)

我认为不是冒号会取代您的文本替换语句。在通过替换Curley大括号使它复杂化之前,先替换“ AND”字符串如何?否则不会影响您的其他替换产品。

var t1 = stringified.replace("AND:[","").replace("}{",", ").replace("}}]}","}}");

答案 2 :(得分:0)

最终的解决方案或更确切地说是我发现的问题是 当josn字符串化时,它在每个单词前加上'\'。例如,当json下面是字符串时。

filter: {where: { AND:[{gender: {IN: "Female"}},{age: {LTE: 44}}]}, relativeDateRange: 90}

它将输出显示为

\filter: {\where: { \AND ....

简单的解决方法是使用如下所示的带有双反斜杠的替换。

var t1 = stringified.replace('{\\"AND\\":','').replace(/}{/g,",").replace("}}},","}},").replace('{/"/":','')
相关问题