因此,对于Lua,我使用它来替换来自Nginx服务器的响应页面的HTML输出。为了得到一个值=" *" HTML标记为空。
问题是,在Lua替换字符串之后,我一直在松开HTML的末尾,并且在引用之间的任何内容之后将其余的HTML清空在同一行之后。
Lua代码:
body_filter_by_lua_block {
local body = ngx.arg[1] --Put body into local var
local htmlvaluetomakeempty = "id=\"username\" value="
local loginpagematch = ngx.re.match(body, "" .. htmlvaluetomakeempty .. "\"(?:.*)\"") --Search through body to see if our html match is found
if loginpagematch then --If not empty
body = ngx.re.gsub(body, "" .. htmlvaluetomakeempty .. "\"(?:.*)\"", "" .. htmlvaluetomakeempty .. "\"\"") --.. loginpagematch["match"] )
ngx.arg[1] = body
end
}
Lua将要替换的HTML代码:
<div class="login-fields"><label id="username-lbl" for="username" class="">User Name</label> <input type="text" name="username" id="username" value="test" class="validate-username" size="25"/></div>
在Lua运行并修改了正文内容之后,输出看起来像这样
<div class="login-fields"><label id="username-lbl" for="username" class="">User Name</label> <input type="text" name="username" id="username" value=""/></div>
问题是此HTML代码也因未知原因被删除
class="validate-username" size="25"
它确实成功地使id="username" value=""
为空但我放弃了HTML之后的所有内容并且我不确定原因。
答案 0 :(得分:1)
使用正则表达式来操作HTML通常是一个失败的原因。即使输入中的空格更改也可能完全破坏您的脚本。建议使用HTML解析库,例如lua-gumbo。
以下示例将找到 func StoresCollectionReceived() {
for store in StoresArray {
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = store
request.region = MapView.region
let search = MKLocalSearch(request: request)
search.start { (response, error) in
if error != nil {
print(error!.localizedDescription)
return
}
if response!.mapItems.count == 0 {
NSLog("No local search matches found for \(store)")
return
}
NSLog("Matches found for \(store)")
self.StartMonitoringGeofenceRegions(mapItems: response!.mapItems)
}
}
}
元素并将其 internal func StartMonitoringGeofenceRegions(mapItems: [MKMapItem]){
if self.userLocation == nil { return }
var possibleRegionsPerStore = Int(round(Double(StoresArray.count / 20)))
possibleRegionsPerStore = possibleRegionsPerStore < 4 ? 4: possibleRegionsPerStore
var itemsCount = 0
itemsCount = TryMonitoreRegion(mapItems: mapItems, possibleRegionsPerStore: possibleRegionsPerStore, itemsCount: itemsCount, minDistance: 0, maxDistance: mapSpan * 0.1)
if itemsCount == possibleRegionsPerStore { return }
itemsCount = TryMonitoreRegion(mapItems: mapItems, possibleRegionsPerStore: possibleRegionsPerStore, itemsCount: itemsCount, minDistance: mapSpan * 0.1, maxDistance: mapSpan * 0.2)
if itemsCount == possibleRegionsPerStore { return }
itemsCount = TryMonitoreRegion(mapItems: mapItems, possibleRegionsPerStore: possibleRegionsPerStore, itemsCount: itemsCount, minDistance: mapSpan * 0.2, maxDistance: mapSpan * 0.3)
if itemsCount == possibleRegionsPerStore { return }
itemsCount = TryMonitoreRegion(mapItems: mapItems, possibleRegionsPerStore: possibleRegionsPerStore, itemsCount: itemsCount, minDistance: mapSpan * 0.3, maxDistance: mapSpan * 0.4)
if itemsCount == possibleRegionsPerStore { return }
itemsCount = TryMonitoreRegion(mapItems: mapItems, possibleRegionsPerStore: possibleRegionsPerStore, itemsCount: itemsCount, minDistance: mapSpan * 0.4, maxDistance: mapSpan * 0.6)
if itemsCount == possibleRegionsPerStore { return }
itemsCount = TryMonitoreRegion(mapItems: mapItems, possibleRegionsPerStore: possibleRegionsPerStore, itemsCount: itemsCount, minDistance: mapSpan * 0.6, maxDistance: mapSpan * 3)
}
private func TryMonitoreRegion(mapItems:[MKMapItem], possibleRegionsPerStore:Int, itemsCount:Int, minDistance:Double, maxDistance:Double) -> Int{
for mapItem:MKMapItem in mapItems{
self.SetAnnotations(mapItem: mapItem)
if itemsCount == possibleRegionsPerStore { return itemsCount }
let distanceToUser = CalculateDistanceBetweenTwoCoordinates(location1: userLocation!, location2: mapItem.placemark.coordinate)
//Monitore 6th nearest stores if regions count still below 20
if locationManager.monitoredRegions.count < 20 && distanceToUser >= minDistance && distanceToUser <= maxDistance {
MonitoreCircularRegion(mapItem: mapItem)
return itemsCount + 1
}
}
return itemsCount
}
private func MonitoreCircularRegion(mapItem: MKMapItem){
DispatchQueue.main.async {
let region = CLCircularRegion(center: mapItem.placemark.coordinate, radius: CLLocationDistance(self.radiusToMonitore), identifier: "\(UUID().uuidString)\("SB_")\(mapItem.name!)")
self.locationManager.startMonitoring(for: region)
NSLog("Monitored Regions: \(self.locationManager.monitoredRegions.count)")
NSLog("MapSpan: \(self.mapSpan)")
NSLog("Start monitoring for Region: \(region) with Radius \(region.radius)" )
}
}
属性设置为空字符串:
input#username