Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9324413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:35:30+00:00 2026-06-19T04:35:30+00:00

I’m working on a JavaScript program that’s parsing data from an API. Part of

  • 0

I’m working on a JavaScript program that’s parsing data from an API. Part of the data is coming back in the format ‘\t’ etc and I need to parse it as tab (in this example) not ‘\t’.

In PHP, there’s a wonderful function called stripcslashes() which does this for me, but I can’t find a JavaScript equivalent.

Does anyone know of such a function in JavaScript? Or a way of parsing ‘\t’ as a tab?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:35:31+00:00Added an answer on June 19, 2026 at 4:35 am

    Javascript’s string literals use those same backslash escapes. There should be a way to make use of that…

    var escapedString = '\\t' // actually contains "\t"
    ,   renderedString = (new Function('return "' + text + '"'))()
    ;
    console.log(renderedString); // "   " <= this is the tab character
    

    Note that this leaves your code wide open for injection attacks, and is very unsafe, especially if the escapedString is coming from an external source!

    It’s (almost) as bad as using eval!

    Update: the safe way

    A better way would probably be using a regular expression:

    var escapedString = '\\t' // actually contains the characters \ and t
    ,   renderedString = escapedString.replace(/\\(.)/g, function (match, char) {
            return {'t': '\t', 'r': '\r', 'n': '\n', '0': '\0'}[char] || match;
        }
    ;
    console.log(renderedString); // "   " <= this is the tab character
    

    Here you’d be using the replace method to track down all the backslashes (followed by any character) in the given string, and call the anonymous function every time a match is found. The function’s returned value will determine what that match will be replaced by.

    The anonymous function contains a JS object (in this case used as an associative array), which contains all the recognized escape characters. The character following the backslash (here called char), will be looked up in that associative array, and JS will return the corresponding value.

    The final part of the line, || char, ensures that if the char is not part of the associative array, the unchanged match is used (which includes the backslash), leaving the original string unchanged.

    Of course, this does mean that you have to specify all the possible escapes in advance.

    Second update: the way to go

    It just occurred to me that the first method is unsafe (there might be fraudulent input), and the second method uncertain (you don’t really know what escapes you need to provide for); perhaps both methods could be combined!

    var escapedString = '\\t' // actually contains the characters \ and t
    ,   renderedString = escapedString.replace(/\\./g, function (match) {
            return (new Function('return "' + match + '"'))() || match;
        }
    ;
    console.log(renderedString); // "   " <= this is the tab character
    

    Of course, the performance of this solution won’t be very great. Parsing and building that new function is a costly thing, and it needs to be done for each and every matching backslash present in the escaped string. If performance becomes an issue, you could add some kind of caching mechanism, which remembers each function once it’s been created.

    Then again, you could also look up the API you need to work with, try and find its documentation or contact its makers, and get a definitive list of escapes it can produce. :-0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.