最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Struggling with regex to match only two of a character, not three - Stack Overflow

programmeradmin3浏览0评论

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

It can't match /// or /

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

which is basically "something that isn't /, followed by // followed by something that isn't /"

The approach seems to work apart from when the string I want to match starts with //

This doesn't work:

//example

This does

stuff // example

How do I solve this problem?

Edit: A bit more context - I am trying to replace // with !, so I am then using:

result = result.replace(myRegex, "$1 ! $2");

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

It can't match /// or /

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

which is basically "something that isn't /, followed by // followed by something that isn't /"

The approach seems to work apart from when the string I want to match starts with //

This doesn't work:

//example

This does

stuff // example

How do I solve this problem?

Edit: A bit more context - I am trying to replace // with !, so I am then using:

result = result.replace(myRegex, "$1 ! $2");
Share Improve this question asked Jan 5, 2011 at 21:05 Fiona - myaccessible.websiteFiona - myaccessible.website 14.9k17 gold badges84 silver badges118 bronze badges 5
  • 2 Do you only need to match // or also the text around it? – Felix Kling Commented Jan 5, 2011 at 21:07
  • I'm trying to replace all occurrences of // with ! – Fiona - myaccessible.website Commented Jan 5, 2011 at 21:20
  • For simple string operations you should use simple string operations like String.replace, not regexes. Matching a (or two) chars qualifies as a simple string operation. – nikc Commented Jan 5, 2011 at 21:23
  • @nikc: He is using String.replace. How would you specify to replace only // but not e.g. /// without regex? – Felix Kling Commented Jan 5, 2011 at 21:25
  • @Felix: ah, my bad, too much whisky. Missed that /// (and friends) should be ignored. – nikc Commented Jan 5, 2011 at 21:28
Add a ment  | 

5 Answers 5

Reset to default 6

Replace two slashes that either begin the string or do not follow a slash, and are followed by anything not a slash or the end of the string.

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

It looks like it wouldn't work for example// either.

The problem is because you're matching // preceded and followed by at least one non-slash character. This can be solved by anchoring the regex, and then you can make the preceding/following text optional:

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

Use negative lookahead/lookbehind assertions:

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

Use this:

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

e.g.

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

EDIT: Updated the expression as per the ment.

/[/]{2}/

e.g:

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

This does not answer the OP's question about using regex, but since some of the original ments suggested using .replaceAll, since not everyone who reads the question in the future wants to use regex, since people might mistakenly assume that regex is the only alternative, and since these details cannot be acmodated by submitting a ment, here's a poor man's non-regex approach:

  1. Temporarily replace the three contiguous characters with something that would never naturally occur — really important when dealing with user-entered values.
  2. Replace the remaining two contiguous characters using .replaceAll().
  3. Return the original three contiguous characters.

For instance, let's say you wanted to remove all instances of ".." without affecting occurrences of "...".

var cleansedText = $(this).text().toString()
    .replaceAll("...", "☰☸☧")
    .replaceAll("..", "")
    .replaceAll("☰☸☧", "...")
; 
$(this).text(cleansedText);

Perhaps not as fast as regex for longer strings, but works great for short ones.

发布评论

评论列表(0)

  1. 暂无评论