我正在开发一个需要更新版权标题的开源项目。目前,每个文件都包含一个具有不同年份的注释标题。有些人正确地说2015年,其他人说从2011年到2014年的任何事情,有些提供日期范围(即2011-2014 - 见附例)。
我想以编程方式将所有过时年份更改为2015年。是否可以仅查找和替换第二年?我目前正在使用此作为我的正则表达式来抓取每个标题:
\/\*(?:(?!\*\/).|[\n\r])*\*\/
这是一个示例标题:
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2011-2013 Company Name AS. All rights reserved.
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* http://someurl.com
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at http://someurl.com/license.html
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*/
答案 0 :(得分:1)
根据@ stribizhev的评论
如果要替换:
2011-2014
变为2011-2015
2014
变为2015
2011 to 2014
变为2011 to 2015
然后你可以使用:
.replace(/(\/\*(?:(?!\*\/)[^])*)\b(-?\d{4} )\b/, "$12015 ")
这不是一个非常狭窄的正则表达式,因为它可以取代超出预期的值。
我建议先在在线正则表达式测试中测试一下。