Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

string.replace 截取字符串 得不到预期的结果?

使用的时同一个方法 截取的结果预期不同

image.png
"moduleType_7556304c7b9f9b325c6e309c1c84d5a2".replace(/^(1*)_/,'')

"functionModuleList_7556304c7b9f9b325c6e309c1c84d5a2".replace(/^(1*)_/,'')

我估计是因为字符串太长导致的 所以我又敲着试了下 结果如下
image.png

但是 "moduleType_7556304c7b9f9b325c6e309c1c84d5a2".replace(/^(1*)_/,'') 能够得到预期的结果 字符串也比前面的长
接着有试了一下给他分别加 a b s 结果又不一样
a b 得到预期结果 加s 就得不到预期

image.png

image.png


  1. s ?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

我觉得你要的应该是/^([^s]*)_/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...