问题 
我需要通过 JS 替换文件中的版本号,我不能只针对我需要的特定部分。我已将文件的内容读入一个变量,并将用修改后的内容覆盖整个文件。该文件如下所示(标准 wordpress style.css): 
- /******************************************************************
 
 - Theme Name: Starter Theme
 
 - Theme URI: 
 
 - Description: Custom Wordpress starter theme
 
 - Author: 
 
 - Author URI:
 
 - Version: 1.0.0
 
 - Tags: fluid-layout, responsive-layout, translation-ready
 
  
- License: WTFPL
 
 - License URI: 
 
 - ------------------------------------------------------------------*/
 
  
- .sticky {}          /* DO NOT EDIT THIS */
 
 - .gallery-caption {} /* THESE ARE USELESS */
 
 - .bypostauthor {}    /* THEY ARE ONLY TO KEEP THEME CHECK HAPPY */
 
  复制代码 
问题是这个版本:1.0.0 
 
有一些细微差别似乎使这变得困难。我只需要更改第三个数字 (0),因为前两个数字 (1.0) 保留用于手动编辑,需要保持原样(所以我不能只替换整个字符串或行)。我一直试图通过子字符串和/或正则表达式仅提取最后一个数字,但似乎找不到正确的方法(因为它周围的内容不是静态的,加上版本字符串结束带有换行符)。任何帮助将不胜感激。 
 
回答 
- let newVersion = 3;
 
 - content = content.replace(/^Version: \d+\.\d+\.\d+$/mg, function(match, index) {
 
 -     return match.replace(/\.\d+$/, '.' + newVersion);
 
 - });
 
  复制代码 
 
 
 
 |