javascript - remove everything before certain character (including)
JavaScript[Edit]
+
0
-
0
JavaScript - remove everything before certain character (including)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23const findText = (text, character) => { for (let i = 0; i < text.length; ++i) { if (text[i] === character) { return text.substring(i + 1); } } return text; }; // Usage example: const text = 'Removed part-example text...'; const character = '-'; const result = findText(text, character); console.log(result); // example text... // Output: // // example text...
[Edit]
+
0
-
0
JavaScript - remove everything before certain character (including)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35const findText = (text, characters, skip = 0) => { let a = -1; for (let i = 0; i <= skip; ++i) { const b = text.indexOf(characters, a); if (b === -1 && skip !== 0) { return null; } a = b + 1; } return text.substring(a); }; // Usage example: const text = 'a-b-c-d'; const character = '-'; console.log(findText('a-b-c-d', '-', 0)); // 'b-c-d' // 0 skipped console.log(findText('a-b-c-d', '-', 1)); // 'c-d' // 1 skipped console.log(findText('a-b-c-d', '-', 2)); // 'd' // 2 skipped console.log(findText('a-b-c-d', '-', 10)); // null // not available - 10 skips console.log(findText('a-b-c-d', '+', 0)); // 'a-b-c-d' // not available - 0 skipped console.log(findText('a-b-c-d', '+', 10)); // null // not available - 10 skips // Output: // // b-c-d // c-d // d // null // a-b-c-d // null
[Edit]
+
0
-
0
JavaScript - remove everything before certain character (including)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22const findText = (text, character) => { const index = text.indexOf(character); if (index !== -1) { return text.substring(index + 1); } return text; }; // Usage example: const text = 'Removed part-example text...'; const character = '-'; const result = findText(text, character); console.log(result); // example text... // Output: // // example text...