How do you convert a string to a character array in JavaScript? I’m thinking getting a string like “Hello world!” to the array [‘H’,’e’,’l’,’l’,’o’,’ ‘,’w’,’o’,’r’,’l’,’d’,’!’] Answer Note: This is not unicode compliant. “I💖U”.split(”) results in the 4 character array [“I”, “�”, “�”, “u”] which can lead to dangerous bugs. See answers below for safe alternatives. Just split it by an
Tag: string
Javascript Equivalent to PHP Explode()
I have this string: 0000000020C90037:TEMP:data I need this string: TEMP:data. With PHP I would do this: How do I effectively explode a string in JavaScript the way it works in PHP? Answer This is a direct conversion from your PHP code:
javascript regular expression to check for IP addresses
I have several ip addresses like: 115.42.150.37 115.42.150.38 115.42.150.50 What type of regular expression should I write if I want to search for the all the 3 ip addresses? Eg, if I do 115.42.150.* (I will be able to search for all 3 ip addresses) What I can do now is something like: /[0-9]{1-3}.[0-9]{1-3}.[0-9]{1-3}.[0-9]{1-3}/ but it can’t seems to work
How do I shuffle the characters in a string in JavaScript?
In particular, I want to make sure to avoid the mistake made in Microsoft’s Browser Choice shuffle code. That is, I want to make sure that each letter has an equal probability of ending up in each possible position. e.g. Given “ABCDEFG”, return something like “GEFBDCA”. Answer I modified an example from the Fisher-Yates Shuffle entry on Wikipedia to shuffle
How can I convert a comma-separated string to an array?
I have a comma-separated string that I want to convert into an array, so I can loop through it. Is there anything built-in to do this? For example, I have this string Now I want to split this by the comma, and then store it in an array. Answer MDN reference, mostly helpful for the possibly unexpected behavior of the
Is there a JavaScript function that can pad a string to get to a determined length?
I am in need of a JavaScript function which can take a value and pad it to a given length (I need spaces, but anything would do). I found this, but I have no idea what the heck it is doing and it doesn’t seem to work for me. Answer ECMAScript 2017 (ES8) added String.padStart (along with String.padEnd) for just
Replace spaces with dashes and make all letters lower-case
I need to reformat a string using jQuery or vanilla JavaScript Let’s say we have “Sonic Free Games”. I want to convert it to “sonic-free-games”. So whitespaces should be replaced by dashes and all letters converted to small letters. Any help on this please? Answer Just use the String replace and toLowerCase methods, for example: Notice the g flag on
Strip all non-numeric characters from string in JavaScript
Consider a non-DOM scenario where you’d want to remove all non-numeric characters from a string using JavaScript/ECMAScript. Any characters that are in range 0 – 9 should be kept. How would you achieve this in plain JavaScript? Please remember this is a non-DOM scenario, so jQuery and other solutions involving browser and keypress events aren’t suitable. Answer Use the string’s
Single quotes in string with jQuery ajax
I have run into a problem where the user enters data and if there are single quotes at all, the script errors out. What’s the best way to handle single quotes that users enter so it doesn’t interfere with the jquery/javascript? UPDATE: I’m sending it through ajax to a database. here is the data parameter for a json ajax call.
How to extract base URL from a string in JavaScript?
I’m trying to find a relatively easy and reliable method to extract the base URL from a string variable using JavaScript (or jQuery). For example, given something like: http://www.sitename.com/article/2009/09/14/this-is-an-article/ I’d like to get: http://www.sitename.com/ Is a regular expression the best bet? If so, what statement could I use to assign the base URL extracted from a given string to a