store ":;',./<>?,./asdaA12 in javascript variable dynamicaly (Unexpected token error)

89 Views Asked by At

I am tying to store ":;',./<>?,./asdaA12 value in js variable which is in gsp page but it could'nt store it properly and gives me run-time error because of special symbol contain " and ' as string. my snap code is :

var editGrp_billingCode_val ="${returnVal?.groupCommand?.billingCode?:billingCode?:billingCode}";

Here Billing code populate with : ":;',./<>?,./asdaA12 value which contain " or ' symbols so for that page gives me run-time js error

is there any one solution for converting this types of string into String variable and store into js variable. like toString method in java

Value ":;',./<>?,./asdaA12 is not fixed it will come dynamically while running the application so i have to convert it at run-time as stored in js variable

2

There are 2 best solutions below

1
On

By escaping them:

<script>
    var x = "\".'*:\","
    alert(x)
    // this should print ".'*:",

</script>

Short escaping list:

  • " = \" or ' = \'
  • \ = \\
  • New Line = \n
  • Tab = \t

more: w3schools - JavaScript Strings

1
On

I've figured it out, just enclose your variable (symbols) in between the "forward slash" e.g

      <script>
                 var x = /".'*:",/
                  alert(x)
                 // this should print ".'*:",

      </script>