Struggling with regex to match only two characters, not three

I need to match all // occurrences in a string in a Javascript regex

It cannot match /// or /

I still have (.*[^\/])\/{2}([^\/].*)

which is basically "something that is not /, followed by //, followed by something that is not /"

The approach seems to work separately from when the line I want to match starts with //

This does not work:

//Example

It does

material // example

How to solve this problem?

Edit: a little more context - I'm trying to replace // with !, so I use:

result = result.replace(myRegex, "$1 ! $2");
+3
source share
4 answers

, ,   , .

s=s.replace(/(^|[^/])\/{2}([^/]|$)/g,'$1!$2');
+5

:

/([^/]*)(\/{2})([^/]*)/g

.

alert("///exam//ple".replace(/([^/]*)(\/{2})([^/]*)/g, "$1$3"));  

EDIT: .

/[/] {2}/

:

alert("//example".replace(/[/]{2}/, ""));  

>

0

, example//.

, //, , , . , , / :

^(.*[^\/])?\/{2}([^\/].*)?$
0

lookahead/lookbehind:

(.*)(?<!/)//(?!/)(.*)

0

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


All Articles