将JS替换为Golang JS Regexp
在JavaScript中,我有一个Word替换函数,我需要转换为Golang,但似乎并没有替换。我相信我在Golang的正则是有问题的。
工作js示例 - 用完整单词代替街道名称缩写。 我不能简单地替换,因为某些单词在字符串的末尾,有些单词在之前和之后都有空白空间,而有些单词在字符串的开头,因此我的不同regexp。
word.replace(/ RD$/, ' ROAD')
.replace(' RD ', ' ROAD ')
.replace(' MKT ', ' MARKET ')
.replace(' NTH ', ' NORTH ')
.replace(/^NTH /, 'NORTH ')
.replace(/ NTH$/, ' NORTH')
.replace(' PK ', ' PARK ')
.replace(/^STH /, 'SOUTH ')
.replace(' STH ', ' SOUTH ')
.replace(' ST ', ' STREET ')
我与Golang的尝试。
type Address struct {
.....
StreetName string
}
func (address *Address) replace(oldWord string, newWord string) {
sampleRegexp := regexp.MustCompile(oldWord)
result := sampleRegexp.ReplaceAllString(string(address.StreetName), newWord)
fmt.Println(oldWord, newWord, result)
address.StreetName = result
}
然后在我的主机上。例如,我称其为这样的,
address.replace(`/ RD$/`, " ROAD")
address.replace(" RD ", " ROAD ")
address.replace(" MKT ", " MARKET ")
address.replace(" NTH ", " NORTH ")
address.replace(`/^NTH /`, "NORTH ")
address.replace(`/ NTH$/`, " NORTH")
address.replace(" PK ", " PARK ")
address.replace(`/^STH /`, "SOUTH ")
例如,如果StreetName为Market Rd
,我希望它成为Market道路
,但没有替换。
可能是因为REGEXP不为字符串
?
我做了一个简单的 Playground
In Javascript I have a word replace function that I need to convert to GoLang, but it doesn't seems to be replacing. I believe there's something wrong with my regex in Golang.
Working JS Example - Replacing street names acronyms with the full word.
I cannot do plain replace because some of the words are at the end of the string, some of the words have white space before and after, and some are at the start of the string, hence my different regexp.
word.replace(/ RD$/, ' ROAD')
.replace(' RD ', ' ROAD ')
.replace(' MKT ', ' MARKET ')
.replace(' NTH ', ' NORTH ')
.replace(/^NTH /, 'NORTH ')
.replace(/ NTH$/, ' NORTH')
.replace(' PK ', ' PARK ')
.replace(/^STH /, 'SOUTH ')
.replace(' STH ', ' SOUTH ')
.replace(' ST ', ' STREET ')
My attempt with GoLang.
type Address struct {
.....
StreetName string
}
func (address *Address) replace(oldWord string, newWord string) {
sampleRegexp := regexp.MustCompile(oldWord)
result := sampleRegexp.ReplaceAllString(string(address.StreetName), newWord)
fmt.Println(oldWord, newWord, result)
address.StreetName = result
}
Then in my main.go I'm calling it like that
address.replace(`/ RD$/`, " ROAD")
address.replace(" RD ", " ROAD ")
address.replace(" MKT ", " MARKET ")
address.replace(" NTH ", " NORTH ")
address.replace(`/^NTH /`, "NORTH ")
address.replace(`/ NTH$/`, " NORTH")
address.replace(" PK ", " PARK ")
address.replace(`/^STH /`, "SOUTH ")
For example if the StreetName is Market Rd
, i expect it to become Market Road
, but it's not replacing.
Could it be because regexp is not of type string
?
I did up a simple playground
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么不使用零length Word Boundare
\ b
?这将把比赛锚定在任何单词边界上。那只是一个问题:
https://go.dev/play/pplay/p/tks1niarzwq
转换
为
Why don't you use the zero-length word boundary
\b
? That will anchor the match at any word boundary.Then it's just a matter of:
https://go.dev/play/p/TKs1nIaRZWQ
which converts
to