开发者

regex to match a character only if not followed by another character, then replace just that initial character

For instance,

(/=[^>]/, '═')

I'd like to keep that match, but only replace the equals sign with the double-horizontal-line. As it is, it matches any '=' that is followed by anything that isn't a '>' but then replaces both the '=' and the following character with the replacing character, I want to keep the following character, but replace just the '='. This is in ruby, i开发者_JS百科f it makes any syntactic difference.

Example input:

= render :partial => 'file'

First = should be converted, second should be preserved


Depending on your regex library (I don't know Ruby), you may be able to use zero-width assertions:

/=(?!>)/

Note that this regex is slightly different to your regex, but it matches the description you gave in the title better. It will match any = that isn't followed by >. This includes matching an = at the end of the text, which your version won't match.


Like this? (I am using - instead of that special character you have):

Inside of irb:

ruby-1.9.2-p0 > "=x".gsub(/=([^>])/, '-\1')
 => "-x" 

ruby-1.9.2-p0 > "=>".gsub(/=([^>])/, '-\1')
 => "=>" 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜