Skip to content

Singe quote strings - issues with special characters that need escaping #3073

@pjanik

Description

@pjanik

Describe the bug
It seems that single quote strings don't work too well with special characters that need to be escaped, like ", or '.

To Reproduce
Open the MathJS website, the webbrowser console, and type:

  1. math.evaluate("'te\"st'")
  2. math.evaluate("'te\'st'")

Result

  1. Uncaught SyntaxError: Unexpected non-whitespace character after JSON at position 4 (line 1 column 5) at JSON.parse ()
  2. SymbolNode.js:109 Uncaught Error: Undefined symbol st at Function.value

Exptected result

  1. 'te"st'
  2. "te'st"

Other comments

It is worth noting that the first issue (1.) can be workaround by typing math.evaluate("'te\\\"st'"). However, the second issue (2.) cannot be fixed by using any number of backslashes.

This problem may be related to the following line:

return JSON.parse('"' + str + '"') // unescape escaped characters

where single-quoted string content is actually enclosed in double quotes. Also, I understand that there are some possible conflicts with the transpose operator, but I am not sure how this could or should affect characters escaping.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions