Quantcast
Channel: Get everything after the dash in a string in JavaScript - Stack Overflow
Browsing all 15 articles
Browse latest View live
↧

Answer by Jonathan for Get everything after the dash in a string in JavaScript

Efficient, compact and works in the general case:s='sometext-20202's.slice(s.lastIndexOf('-')+1)

View Article


Answer by Bhanusri Kokala for Get everything after the dash in a string in...

To use any delimiter and get first or second part //To divide string using deimeter - here @ //str: full string that is to be splitted //delimeter: like '-' //part number: 0 - for string befor...

View Article

Answer by aturan23 for Get everything after the dash in a string in JavaScript

You can use split method for it. And if you should take string from specific pattern you can use split with req. exp.:var string = "sometext-20202";console.log(string.split(/-(.*)/)[1])

View Article

Answer by axwr for Get everything after the dash in a string in JavaScript

Everyone else has posted some perfectly reasonable answers. I took a different direction. Without using split, substring, or indexOf. Works great on i.e. and firefox. Probably works on Netscape...

View Article

Answer by Steve for Get everything after the dash in a string in JavaScript

For those trying to get everything after the first occurrence:Something like "Nic K Cage" to "K Cage".You can use slice to get everything from a certain character. In this case from the first...

View Article


Answer by Leon Williams for Get everything after the dash in a string in...

I came to this question because I needed what OP was asking but more than what other answers offered (they're technically correct, but too minimal for my purposes). I've made my own solution; maybe...

View Article

Answer by Mahyar Ekramian for Get everything after the dash in a string in...

With built-in javascript replace() function and using of regex (/(.*)-/), you can replace the substring before the dash character with empty string (""):"sometext-20202".replace(/(.*)-/,""); // result...

View Article

Answer by kcsoft for Get everything after the dash in a string in JavaScript

myString.split('-').splice(1).join('-')

View Article


Answer by Kanke for Get everything after the dash in a string in JavaScript

var testStr = "sometext-20202"var splitStr = testStr.substring(testStr.indexOf('-') + 1);

View Article


Answer by RPL for Get everything after the dash in a string in JavaScript

A solution I prefer would be:const str = 'sometext-20202';const slug = str.split('-').pop();Where slug would be your result

View Article

Answer by artlung for Get everything after the dash in a string in JavaScript

How I would do this:// function you can use:function getSecondPart(str) { return str.split('-')[1];}// use the function:alert(getSecondPart("sometext-20202"));

View Article

Answer by Cerebrus for Get everything after the dash in a string in JavaScript

AFAIK, both substring() and indexOf() are supported by both Mozilla and IE. However, note that substr() might not be supported on earlier versions of some browsers (esp. Netscape/Opera).Your post...

View Article

Answer by dirkgently for Get everything after the dash in a string in JavaScript

Use a regular expression of the form: \w-\d+ where a \w represents a word and \d represents a digit. They won't work out of the box, so play around. Try this.

View Article


Answer by Sean Bright for Get everything after the dash in a string in...

var the_string = "sometext-20202";var parts = the_string.split('-', 2);// After calling split(), 'parts' is an array with two elements:// parts[0] is 'sometext'// parts[1] is '20202'var the_text =...

View Article

Get everything after the dash in a string in JavaScript

What would be the cleanest way of doing this that would work in both IE and Firefox?My string looks like this sometext-20202Now the sometext and the integer after the dash can be of varying...

View Article

Browsing all 15 articles
Browse latest View live




<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>