Javascript字符串替换方法.在替换字符串中使用匹配项 [英] Javascript string replace method. Using matches in replacement string

查看:49
本文介绍了Javascript字符串替换方法.在替换字符串中使用匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下javascript字符串

I have the following javascript string

/Library/Application%20Support/Adobe/Fireworks%20CS6/Commands

"CS#"部分中的数字可能会更改,但是我要记住字符串可能会发生变化,但我想将字符串转换为以下格式.

The numeral in the "CS#" section may change, but I would like to convert the string to the following format keeping in mind that the numeral may change.

/Library/Caches/Adobe/TypeSupport/CS6

我知道我可以通过多种方式做到这一点,但是出于教育的目的,我正在寻找最好的方法.

I know I can do this in several ways, but for the purpose of education I am looking for the best method.

我可以使用replace方法使用正则表达式进行搜索,但是可以在替换中使用匹配项吗?

I can search with a regular expression using the replace method, but is it possible to use matches in the replacement?

感谢您的帮助

推荐答案

最简单的解决方案是在正则表达式中使用括号来捕获要在替换中使用的原始字符串部分,然后将其包括在通过特殊序列 $ 1 替换字符串.例如:

The simplest solution is to use parentheses in the regular expression to capture the part of the original string you wish to use in the replacement, and then include that in the replacement string via the special sequence $1. For your example:

'/Library/Application%20Support/Adobe/Fireworks%20CS6/Commands'.replace(
    /^.*\/(CS[0-9]+).*$/, 
    '/Library/Caches/Adobe/TypeSupport/$1'); 
// => /Library/Caches/Adobe/TypeSupport/CS6

(您可以有多个括号,在这种情况下,后面的组是 $ 2 $ 3 等.有时,您关心的字符串恰好是与整个正则表达式匹配的内容,在这种情况下,您只需在替换字符串中使用 $& 即可,而无需任何括号.)

(You can have more than one set of parentheses, in which case subsequent groups are $2, $3, etc. Sometimes the string you care about happens to be exactly what matches the entire regular expression, in which case you can just use $& in the replacement string , and don't need any parentheses.)

这非常适合在执行操作时仅包含源字符串中的文字文本.

This is great for simply including literal text from the source string, as you're doing.

但是,如果您做的事情有些夸张,例如尝试更改匹配文本的大小写,那么它将不起作用.('$ 1'.toLowerCase()只会将文字字符串'$ 1'转换为小写,然后再传递给 replace ;这无济于事.)在这种情况下,您可以使用一个函数代替字符串代替.每次匹配将调用一次,并传递匹配的字符串部分(相当于字符串版本中的 $& );如果正则表达式中包含括号,则会按顺序传递一个包含每个组的匹配项的附加参数.因此,您可以将示例转换为小写:

If you're doing anything a bit fancier, however - such as trying to change the case of the matched text - it won't work. ('$1'.toLowerCase() will simply convert the literal string '$1' to lowercase before passing it to replace; not helpful.) For those situations, you can use a function instead of a string as the replacement. It will be called once per match, and passed the part of the string that matched (equivalent to $& in the string version); if there are parentheses in the regex, it will be passed an additional parameter containing the match for each group, in order. So you could convert your example to lowercase like this:

'/Library/Application%20Support/Adobe/Fireworks%20CS6/Commands'.replace(
        /^.*\/(CS[0-9]+).*$/, 
        function(whole_match, cs_number) { 
          return "/Library/Caches/Adobe/type_support/" + cs_number.toLowerCase()
        });  
// => /Library/Caches/Adobe/type_support/cs6

您可以在此处.

这篇关于Javascript字符串替换方法.在替换字符串中使用匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆