0

I have a string as below

var a = "M=1234&b=sdaks&c=sdkssad&strXML=<a><b mode="abc"><c>string content</c></b></a>"

Then I split it with &

var b = a.split('&');

Then further more I split b with = in loop and append to the form

$.each(b, function (index) {
        var paramsV = b[index].split('=');
        frm.append('<input type="hidden" name="' + paramsV[0] + '" value="' + paramsV[1] + '" /> ');
    });

But when it split with = that time it is splitting result string which have = inside the string and it is splitting that too. I would like to know how I can stop splitting the result string.

4
  • 1
    The issue is that the split string still has the '=' symbol? Commented Jul 20, 2017 at 6:09
  • Try something like .split(/=(.+)/) Commented Jul 20, 2017 at 6:09
  • there seems to be a typo. can show us what is your expected and current output? Commented Jul 20, 2017 at 6:11
  • I suggest to reduce your efforts by properly encoding url safe parameters, so that decoding them will be lot more easier Commented Jul 20, 2017 at 6:15

3 Answers 3

1

I this what you want to achieve, Use .split(/=(.+)/) to only split the first =

Also notice that when you try to enter paramsV[1] that has a " inside it, it will break the original code of Input, that's why I replaces it with .replace(/\"/g, "''")

var a = 'M=1234&b=sdaks&c=sdkssad&strXML=<a><b mode="abc"><c>string content</c></b></a>'

var b = a.split('&');
var frm = $(".frm")
$.each(b, function(index) {
  var paramsV = b[index].split(/=(.+)/);
  frm.append('<input type="" name="' + paramsV[0] + '" value="' + paramsV[1].replace(/\"/g, "''") + '" /> ');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="frm"></div>

Sign up to request clarification or add additional context in comments.

Comments

1

You have made a mistake in variable declaration. Inside double quotes you need to use single quotes. And you need to use regular expression like .split(/=(.+)?/). See the output below.

$(function(){
var a = "M=1234&b=sdaks&c=sdkssad&strXML=<a><b mode='abc'><c>string content</c></b></a>";
var b = a.split('&');
$.each(b, function (index) {
        var paramsV = b[index].split(/=(.+)?/);
        console.log('<input type="hidden" name="' + paramsV[0] + '" value="' + paramsV[1] + '" /> ');
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

3 Comments

This is what you are looking? @Milind
When I split second time with = that time if you see there is strXML, that strXML have = insdide the string which is also getting split
check now? @Milind
0

var a = "M=1234&b=sdaks&c=sdkssad&strXML=<a><b mode='abc'><c>string content</c></b></a>";
    var b = a.split('&');
    $.each(b, function (index) {
        var index1 = b[index].indexOf("=")
        var markup ='<input type="hidden" name="' + b[index].substring(0, index1) + '" value="' + b[index].substring(index1) + '" /> ';
        console.log(markup);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Check it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.