javascript substr example
Example 1: js get first 3 characters of string
const string = "0123456789";
console.log(string.slice(0, 2));
console.log(string.slice(0, 8));
console.log(string.slice(3, 7));
Example 2: substr() javascript
const str = 'substr';
console.log(str.substr(1, 2));
console.log(str.substr(1));
console.log(str.substr(-3, 2));
console.log(str.substr(-3));
Example 3: js substr
var str = 'abcdefghij';
console.log('(1, 2): ' + str.substr(1, 2));
console.log('(-3, 2): ' + str.substr(-3, 2));
console.log('(-3): ' + str.substr(-3));
console.log('(1): ' + str.substr(1));
console.log('(-20, 2): ' + str.substr(-20, 2));
console.log('(20, 2): ' + str.substr(20, 2));
Example 4: cut string from string javascript
var ret = "data-123".replace('data-','');
console.log(ret);