How to use regex to remove [az] / [az] / from a string?

Say that string abc/xyz/IMPORTANT/DATA/@#!%@!%and I just wantIMPORTANT/DATA/!%#@%!#%

I'm terrible at regex and haven't really learned the JavaScript API yet

+3
source share
3 answers

Use indexOfand substring. You do not need regex for this.

You can use the option startto indexOfsearch from a given position. If you search after the first /, you can find the index of the second /.

s = "abc/def/ghi/jkl";
s = s.substring(s.indexOf('/', s.indexOf('/') + 1) + 1);
document.writeln(s); // "ghi/jkl"

Note that this assumes there will always be at least two /. If this does not happen, it will save sas is.

s = "abc/xyz";
s = s.substring(s.indexOf('/', s.indexOf('/') + 1) + 1);
document.writeln(s); // "abc/xyz"

regex, :

s = "abc/def/ghi/jkl";
s = s.replace(/[a-z]+\/[a-z]+\//, '');
document.writeln(s); // "ghi/jkl"

, , /.

+2

, substring indexOf, , "/".

/([^\/]+\/[^\/]+\/)/, . /^([^\/]+\/[^\/]+\/)/, .

+1

I think,

yourstring.replace(/[a-z]+\/[a-z]+\/(.+)/g, "$1");
+1
source

Source: https://habr.com/ru/post/1747869/


All Articles