Created
August 11, 2020 16:57
-
-
Save zzjtnb/916cfec9001da97cda6c403ca5a57c7e to your computer and use it in GitHub Desktop.
javascript - 使用正则表达式从路径中提取文件名
This file contains hidden or 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
| # 如何使用正则表达式从下面的URL中提取字符串“ XMLFileName” | |
| ## 可以使用split(),pop()和replace() ... | |
| ```JavaScript | |
| var x = "C:\\Documents and Settings\\Dig\\Desktop\\XMLFileName.xml"; | |
| var filename = x.split('\\').pop().replace(/\..+$/, ''); | |
| console.log(filename) | |
| ``` | |
| [jsbin](https://jsfiddle.net/h8uK6) | |
| ## 也可以使用单个正则表达式 | |
| ```JavaScript | |
| var x = "C:\\Documents and Settings\\Dig\\Desktop\\XMLFileName.xml"; | |
| var filename = x.replace(/.*\\|\..*$/g, ''); | |
| console.log(filename) ; | |
| ``` | |
| [jsbin](http://jsfiddle.net/3xdrX) | |
| ==确保在字符串文字中也转义\。== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment