-
-
Save hunandy14/f10d73d3ddaa2b0cb7daf863a06ff943 to your computer and use it in GitHub Desktop.
抓取""中的特定字串
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(s1=="123" || s2=="321") { | |
sql="select xxx || xxx substr(xx || xx)" | |
var a="123"; a.substr(); var b="321" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$srcPath = "a.js" | |
$dstPath = "b.js" | |
New-Item -ItemType File -Path $dstPath -Force | Out-Null | |
# ============================================================================== | |
$ct = (Get-Content $srcPath) | |
$ct_dst = $ct -match '(?<=\").*?(?=\")' | |
## cv1 = replace輸入條件(特殊符號加上斜線) | |
$ct1 = $ct_dst -replace '\|\|', '\|\|' ` | |
-replace '\(', '\(' ` | |
-replace '\)', '\)' ` | |
-replace '\[', '\[' ` | |
-replace '\]', '\]' ` | |
-replace '\{', '\}' ` | |
-replace '\{', '\}' ` | |
-replace '\+', '\+' ` | |
-replace '\-', '\-' ` | |
-replace '\*', '\*' ` | |
-replace '\/', '\/' | |
## 把抓出來有""的行中,目標字串替換 | |
$ct2 = $ct_dst -replace '\|\|', '+' | |
$ct2 = $ct_dst -replace 'substr\(', 'substring(' | |
## 測試 | |
# $ct1 > "zOut\out_1.js" | |
# $ct2 > "zOut\out_2.js" | |
## ct2 是已經替換完成的行,ct1是還沒替換的行,ct是整份文本 (為什麼要繞這麼大一圈是因為我能抓出 || 也能抓出 "" ,但是抓不出 "" 中的 ||) | |
for ($i = 0; $i -lt $ct1.Count; $i++) { | |
$ct = $ct -replace $ct1[$i], $ct2[$i] | |
} $ct_out = $ct | |
# 輸出檔案 | |
$ct2 | |
$ct_out | Out-File $dstPath -Encoding default | |
# ============================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment