簡體   English   中英

正則表達式在xml標記中查找屬性並替換其值(節點/ Javascript)

[英]Regex to find attribute in xml tag and replace its value (node/Javascript)

我有一個Cordova config.xml文件,我正在編寫一個腳本來提高版本。 該文件的示例是:

<?xml version="1.0" encoding="utf-8"?>
<widget android-packageName="com.demo.android" id="com.demo.ios" ios-CFBundleIdentifier="com.demo.ios.dev" version="1.16.6" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name short="DEMO DEV">DEMO</name>

我想替換<widget>標記內的屬性version的值,但不影響所有其他屬性,包括<?xml標記中的屬性。

因此,只需放置,查找並替換widget標記內的屬性version的值即可。

您可以嘗試以下正則表達式並在regex101對其進行測試:

/(<widget [\S\s]*?version=")[^"]+("[\S\s]*?>)/gmi

簡而言之,我在做什么:

  1. 將所有內容分組,從<widgetversion="
  2. 選擇除"
  3. 分組所有內容,直到下一個>

然后,您可以將其替換為$1(new version)$2

這是一個簡單的演示:

 const versionRegex = /(<widget [\\S\\s]*?version=")[^"]+("[\\S\\s]*?>)/gmi; const content = `<?xml version="1.0" encoding="utf-8"?> <widget android-packageName="com.demo.android" id="com.demo.ios" ios-CFBundleIdentifier="com.demo.ios.dev" version="1.16.6" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0"> <name short="DEMO DEV">DEMO</name>` const newVersion = 'testVersion'; const replaced = content.replace( versionRegex, `$1${ newVersion }$2` ) document.getElementById( 'result' ).innerText = replaced; 
 pre { white-space: pre-line; } 
 <pre id="result"></pre> 

這是您想要的:

let xml; // Your xml file
const version = "0.8.5"
xml = xml.replace(/(<widget.+version=")([0-9\.]+)(".*>)/, "$1"+version+"$3")

正則表達式匹配說明:

  • 正則表達式( $1 )的第一個匹配項:第一個字符串,以<widget和所有字符開頭,直到version="
  • 第二個匹配項( $2 ,不在替換中,因為這是我們要替換的字符串)是版本號
  • 最后一個匹配是所有字符,直到小部件屬性的插入符關閉>
  • 全球比賽是這三場比賽

嘗試

查找/<widget(?=\\s)(?=((?:[^>"']|"[^"]*"|'[^']*')*?\\sversion\\s*=\\s*)(?:(['"])([\\S\\s]*?)\\2)((?:"[\\S\\s]*?"|'[\\S\\s]*?'|[^>]*?)*?>))\\s+(?:"[\\S\\s]*?"|'[\\S\\s]*?'|[^>]*?)+>/

替換<widget$1$2NEW_VERSION$2$4

https://regex101.com/r/1KnW0M/1

(如果需要,舊版本在Capture Group 3中。)

更多信息

 # Begin Widget tag

 < widget                 
 (?= \s )
 (?=                           # Asserttion (a pseudo atomic group)
      (                             # (1 start), Up to Version attribute
           (?: [^>"'] | " [^"]* " | ' [^']* ' )*?
           \s version \s* = \s* 
      )                             # (1 end)
      (?:
           ( ['"] )                      # (2), Quote
           ( [\S\s]*? )                  # (3), Version Value
           \2 
      )
      (                             # (4 start), After Version attribute
           (?: " [\S\s]*? " | ' [\S\s]*? ' | [^>]*? )*?
           >
      )                             # (4 end)
 )

 # Have the version, just match the rest of tag

 \s+ 
 (?: " [\S\s]*? " | ' [\S\s]*? ' | [^>]*? )+

 >                             # End Widget tag

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM