I’m having trouble trying to find a substring within a string. This isn’t a simple substring match using indexOf or match() or test() or includes(). I’ve tried using these but to no avail. I have a bunch of strings inside an array, and then either need to use filter() method or the some() method to find a substring match. I
Tag: substring
How to get text after the 9th occurrence of a character using javascript?
I got the following string: NPA-Woodburn,OR,Woodburn,OR,97071,Bensenville,IL,60106,150.00,0.00,cash/certified funds,,enclosed,operable,,,,, I need to loop through some rows and sum the 9th text after “,”, but I just can’t get to it. I’ve seen many combinations of solutions, but none gets me there. Thank you. Answer You can split the string by that character, remove all items in the array before the 9th item
Parsing error message using substr and indexOf not working
I’m trying to extract a substring from an error message I receive and parse it to JSON. However it seems something with the indexOf or the substring method is not working as expected. Here’s the full error message I’m trying to parse: Basically I’m trying to extract this JSON string programatically: To do this I wanted to use a combination
What value does a substring have if it doesn’t exist?
I have a string, msg. I want to check if the third character in the string exists or not. I’ve tried if (msg.substring(3, 4) == undefined) { … } but this doesn’t work. What would the substring be equal to if it does not exist? Answer The third character of the string exists if the string has three characters, so
Javascript arrays email substring into full name, firstname and lastname
I am practicing javascript. I have arrays of emails. from that email I want to get three out puts of strings fullname, firstname and lastname inside one map function. I can able to get output first name by reading one stack-overflow questions. I am able get full name by I get output john.doe, I want johndoe. Could not figure it
Javascript – How to check if a string contains multiple substrings
I have multiple sub-strings that I want to find in a single string and if all three are found then do this, if not, do something else. I am kind of stuck on how to set it so that if I get three “True”, I want to execute something, other I want it to do something else. Many thanks. My
Substring text with HTML tags in Javascript
Do you have solution to substring text with HTML tags in Javascript? For example: Answer Taking into consideration that parsing html with regex is a bad idea, here is a solution that does just that 🙂 EDIT: Just to be clear: This is not a valid solution, it was meant as an exercise that made very lenient assumptions about the
How to check if a string contains text from an array of substrings in JavaScript?
Pretty straight forward. In javascript, I need to check if a string contains any substrings held in an array. Answer There’s nothing built-in that will do that for you, you’ll have to write a function for it, although it can be just a callback to the some array method. Two approaches for you: Array some method Regular expression Array some
How to trim a file extension from a String in JavaScript?
For example, assuming that x = filename.jpg, I want to get filename, where filename could be any file name (Let’s assume the file name only contains [a-zA-Z0-9-_] to simplify.). I saw x.substring(0, x.indexOf(‘.jpg’)) on DZone Snippets, but wouldn’t x.substring(0, x.length-4) perform better? Because, length is a property and doesn’t do character checking whereas indexOf() is a function and does character