Table of Contents
回复内容:
Home Backend Development PHP Tutorial javascript - 正则替换replace中$1的用法

javascript - 正则替换replace中$1的用法

Jun 06, 2016 pm 08:21 PM
javascript php

<code>preg_replace("/([&amp;?]uid=)[^?&amp;]*/",'$1173','http://localhost/mobile/index.php?c=wish&amp;uid=181')

//输出:http://localhost/mobile/index.php?c=wish73
</code>
Copy after login
Copy after login

如上面$replacement ($1173)表示的,php以为我要把$11加上73,但其实我是想让 $1加上173,请问怎样改才能符合我的预期?

刚刚在写问题的时候突然想到把$replacement改成$01说不定可以,没想到真可以了,现在是这样'$01173'

另外,同样的$replacement($1173)放到js里表现的结果却是我预期的,那js里想要$11加上73怎么办?

回复内容:

<code>preg_replace("/([&amp;?]uid=)[^?&amp;]*/",'$1173','http://localhost/mobile/index.php?c=wish&amp;uid=181')

//输出:http://localhost/mobile/index.php?c=wish73
</code>
Copy after login
Copy after login

如上面$replacement ($1173)表示的,php以为我要把$11加上73,但其实我是想让 $1加上173,请问怎样改才能符合我的预期?

刚刚在写问题的时候突然想到把$replacement改成$01说不定可以,没想到真可以了,现在是这样'$01173'

另外,同样的$replacement($1173)放到js里表现的结果却是我预期的,那js里想要$11加上73怎么办?

试试$01173

JS只是比较智能一点,它发现你的正则里面没有11个分组,所以会把$1173拆分为$1173两个部分。但是当分组数量达到11个以上时它也就会以能够匹配到的最大的分组来替换了,比如:

'abcdefffgg'.replace(/(f+)fgg/, '$1173'); // abcdeff173,(替换的是第1个分组,ff)
'abcdefffgg'.replace(
    /(f+)(a?)(a?)(a?)(a?)(a?)(a?)(a?)(a?)(a?)(fgg)/,
    '$1173'); // abcdefgg73(替换的是第11个分组,fgg)
Copy after login

要想在上面的第二个例子里替换第一个分组,可以用$01173,由于分组最大只能到99,而01已经满了2位数,所以JS不会再去尝试检查第三位数。你可以在PHP里试试这种方法,我没测试,不知道可不可行。

至于你说的,想让JS在不足11个分组的时候匹配$11应该是做不到的。

在replace第二个参数里面$实际上是告诉正则表达式引擎,接下来的是一个反向引用(有的语言或工具里面也用\标识),是对正则表达式中匹配出来的组的引用,在你的例子里面就是 ([&amp;?]uid=)所匹配出来的内容。

我对PHP不是很熟。
回答你的javascript的问题,在js里面$1代表的第一个被匹配到的group。所以结果是正确。

如果你想要$11这个字符串的加上73的话,用这个就可以了$$1173

详细说明可参照这里:http://deerchao.net/tutorials/regex/regex.htm#backreference

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles