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() me…
Tag: indexof
Finding position of dom node in the document source
Context I’m building a set of ‘extractor’ functions whose purpose is to extract what looks like components from a page (using jsdom and nodejs). The final result should be these ‘component’ objects ordered by where they originally appeared in the page. Problem The last part of th…
JavaScript String indexOf() selects also 1&3 instead of only 13
I have this indexOf(), the issue I have is that when norms[] has an dataset like 2,13 that options that are set as selected in norm_id[‘+nr+’][] are not only the values 2 and 13 but also the values 1 and 3 Any suggestions how to solve this? Answer I have solved the issue with the code below. @Andr…
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 ge…
How to get an index of an array element within a sort comparison function?
I am looking at this question: The sort method for arrays can take an argument that is a comparison function with two parameters—say, x and y. The function returns a negative integer if x should come before y, zero if x and y are indistinguishable, and a positive integer if x should come after y. Write calls,…
Check if url contains blacklisted strings javascript
If i have an array of blacklisted words like so: and I’d like to check if myLink I got through a.getAttribute(“href”) contains one of those words what’d be the best way to go? Right now i’m trying this but it doesn’t work: full section: Answer Are you in a situation where y…
indexOf() when array-elements are objects (javascript)
For instance, a variable named arrayElements of type array contains: [{id:1, value:5},{id:2, value:6},{id:3, value:7},{id:4, value:8}]. How do I get the position of the array element with id === 3(3rd element) in the arrayElements variable besides using loop? thanks. Answer You have to loop at one point. But …
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 b…
Is there a version of JavaScript’s String.indexOf() that allows for regular expressions?
In javascript, is there an equivalent of String.indexOf() that takes a regular expression instead of a string for the first first parameter while still allowing a second parameter ? I need to do something like and While String.search() takes a regexp as a parameter it does not allow me to specify a second arg…