如何将这些JS代码转换成JAVA代码。尤其是中间的正则表达式。高手帮帮忙啊。很棘手 由于时间紧迫
高手帮帮忙啊functiongetUrl(url,oldFormat){varurlRegex;if(oldFormat=="false"){urlRegex=newRe...
高手帮帮忙啊
function getUrl(url, oldFormat)
{
var urlRegex;
if (oldFormat == "false")
{
urlRegex = new RegExp("(https?://[^/]*(/[^=]*/)*)(.*)");
}
else
{
urlRegex = new RegExp("https?://[^?]*");
}
var urlTmp = url.match (urlRegex);
if (oldFormat == "false")
{
return urlTmp[1];
}
return urlTmp[0];
}
function getQueryParams(url, oldFormat)
{
var splitRegex;
if (oldFormat == "false")
{
splitRegex = new RegExp ("(/)([^/]+)");
}
else
{
splitRegex = new RegExp ("(\\?|&)([^(&|\\?)]+)");
}
var splitTmp = url.split (splitRegex);
var retval = new Array();
var j = 0;
var paramRegex;
if (oldFormat == "false")
{
paramRegex = new RegExp ("[^(/|&|=)]+=[^(/|&)]*");
}
else
{
paramRegex = new RegExp ("[^(&|=)]+=[^&]*");
}
for (var i = 0; i < splitTmp.length; i++)
{
var paramTmp = splitTmp[i].match (paramRegex);
if (paramTmp)
{
var splitParam = paramTmp[0].split('=');
retval[j] = new Array(splitParam[0], splitParam[1]);
j++;
}
}
return retval;
} 展开
function getUrl(url, oldFormat)
{
var urlRegex;
if (oldFormat == "false")
{
urlRegex = new RegExp("(https?://[^/]*(/[^=]*/)*)(.*)");
}
else
{
urlRegex = new RegExp("https?://[^?]*");
}
var urlTmp = url.match (urlRegex);
if (oldFormat == "false")
{
return urlTmp[1];
}
return urlTmp[0];
}
function getQueryParams(url, oldFormat)
{
var splitRegex;
if (oldFormat == "false")
{
splitRegex = new RegExp ("(/)([^/]+)");
}
else
{
splitRegex = new RegExp ("(\\?|&)([^(&|\\?)]+)");
}
var splitTmp = url.split (splitRegex);
var retval = new Array();
var j = 0;
var paramRegex;
if (oldFormat == "false")
{
paramRegex = new RegExp ("[^(/|&|=)]+=[^(/|&)]*");
}
else
{
paramRegex = new RegExp ("[^(&|=)]+=[^&]*");
}
for (var i = 0; i < splitTmp.length; i++)
{
var paramTmp = splitTmp[i].match (paramRegex);
if (paramTmp)
{
var splitParam = paramTmp[0].split('=');
retval[j] = new Array(splitParam[0], splitParam[1]);
j++;
}
}
return retval;
} 展开
1个回答
展开全部
String getUrl(String url, String oldFormat) {
Pattern urlRegex;
if ("false".equals(oldFormat)) {
urlRegex = Pattern.compile("(https?://[^/]*(/[^=]*/)*)(.*)");
} else {
urlRegex = Pattern.compile("https?://[^?]*");
}
Matcher urlTmp = urlRegex.matcher(url);
if (!urlTmp.find()) {
return "";
}
if ("false".equals(oldFormat)) {
return urlTmp.group(1);
}
return urlTmp.group();
}
Map<String, String> getQueryParams(String url, String oldFormat) {
Pattern splitRegex;
if ("false".equals(oldFormat)) {
splitRegex = Pattern.compile("(/)([^/]+)");
} else {
splitRegex = Pattern.compile("(\\?|&)([^(&|\\?)]+)");
}
Matcher splitTmp = splitRegex.matcher(url);
Map<String, String> retval = new HashMap<String, String>();
int j = 0;
Pattern paramRegex;
if (oldFormat == "false") {
paramRegex = Pattern.compile("[^(/|&|=)]+=[^(/|&)]*");
} else {
paramRegex = Pattern.compile("[^(&|=)]+=[^&]*");
}
for (int i = 0; i < splitTmp.groupCount(); i++) {
Matcher paramTmp = paramRegex.matcher(splitTmp.group(i));
if (paramTmp.find()) {
String[] splitParam = paramTmp.group().split("=");
retval.put(splitParam[0], splitParam[1]);
j++;
}
}
return retval;
}
Pattern urlRegex;
if ("false".equals(oldFormat)) {
urlRegex = Pattern.compile("(https?://[^/]*(/[^=]*/)*)(.*)");
} else {
urlRegex = Pattern.compile("https?://[^?]*");
}
Matcher urlTmp = urlRegex.matcher(url);
if (!urlTmp.find()) {
return "";
}
if ("false".equals(oldFormat)) {
return urlTmp.group(1);
}
return urlTmp.group();
}
Map<String, String> getQueryParams(String url, String oldFormat) {
Pattern splitRegex;
if ("false".equals(oldFormat)) {
splitRegex = Pattern.compile("(/)([^/]+)");
} else {
splitRegex = Pattern.compile("(\\?|&)([^(&|\\?)]+)");
}
Matcher splitTmp = splitRegex.matcher(url);
Map<String, String> retval = new HashMap<String, String>();
int j = 0;
Pattern paramRegex;
if (oldFormat == "false") {
paramRegex = Pattern.compile("[^(/|&|=)]+=[^(/|&)]*");
} else {
paramRegex = Pattern.compile("[^(&|=)]+=[^&]*");
}
for (int i = 0; i < splitTmp.groupCount(); i++) {
Matcher paramTmp = paramRegex.matcher(splitTmp.group(i));
if (paramTmp.find()) {
String[] splitParam = paramTmp.group().split("=");
retval.put(splitParam[0], splitParam[1]);
j++;
}
}
return retval;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询