JavaScript 正则匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8531249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:45:10  来源:igfitidea点击:

JavaScript regex matching

javascriptregex

提问by AabinGunz

I have some variable

我有一些变量

var jdbcurl="jdbc:oracle:thin:%2F%2Finnova:1521%3BServiceName%3Dorcl%3BMaxPooledStatements%3D20%3BCatalogOptions%3D0%3BBatchPerformanceWorkaround%3Dtrue"

alert(jdbcurl.match(/:[\d]+/));    //gives me :1521

How can I get jdbc:oracle:thin, innova, 1521& orclout of jdbcurlvar?

如何从var 中获取jdbc:oracle:thin, innova, 1521& ?orcljdbcurl

Update

更新

You can experiment here(if needed)

您可以在这里进行实验(如果需要)

回答by Zack Burt

var jdbcurl="jdbc:oracle:thin:%2F%2Finnova:1521%3BServiceName%3Dorcl%3BMaxPooledStatements%3D20%3BCatalogOptions%3D0%3BBatchPerformanceWorkaround%3Dtrue"
var myregex = /([a-z:]+):%2F%2F([a-z]+):(\d+)%3BServiceName%3D([a-z]+)%3BMaxPooledStatements%3D20%3BCatalogOptions%3D0%3BBatchPerformanceWorkaround%3Dtrue/
var matches = myregex.exec(jdbcurl);
// jdbc:oracle:thin is in matches[1], innova is in matches[2], 1521 is in matches[3], and orcl is in matches[4]

回答by maxRoald

you could also try this for better url readability during regexp maintenance if you have to parse several urls:

如果您必须解析多个 url,您也可以在正则表达式维护期间尝试此操作以获得更好的 url 可读性:

var jdburl = unescape("jdbc:oracle:thin:%2F%2Finnova:1521%3BServiceName%3Dorcl%3BMaxPooledStatements%3D20%3BCatalogOptions%3D0%3BBatchPerformanceWorkaround%3Dtrue");

var myRegExp = new Regexp('([a-z:]+)://(\w+):(\d+);ServiceName=(\w+);');

var matches = myRegExp.exec(jdburl);

回答by pagid

I'd say jdbcurl.split(/(%..)/)would be a start - and then you could check the elements on whether to keep them or split them even further.

我会说jdbcurl.split(/(%..)/)这将是一个开始 - 然后你可以检查元素是保留它们还是进一步拆分它们。

回答by 1983

Use decodeURIComponent() first, then split on semicolons. Don't make it hard on yourself!

首先使用 decodeURIComponent(),然后用分号分割。不要为难自己!