用其他子域替换子域名使用JavaScript?

时间:2013-04-18 14:08:32

标签: javascript string replace

我正在尝试将子域名从“ news .domain.com / path / ..”替换为“ mobile .domain.com / path /。 。“,使用JavaScript

知道如何实现这个目标吗?

6 个答案:

答案 0 :(得分:4)

我假设您要将通用格式xxxx.domain.com/...中的字符串更改为mobile.domain.com/...。这个正则表达式应该在JavaScript中完成:

var oldPath = "news.domain.com/path/";
var newPath = oldPath.replace(/^[^.]*/, 'mobile')

答案 1 :(得分:2)

这应该在正常情况下起作用:

with memo as
(
  select memoid
  from tblmemodetails
  where fktruckid = 5
)
select memonumber, tripid, tripamount, overtime, repairing, lu
from
(
  -- -------------------------------------------------------------------------------
  -- The memos' trips. They have a trip amount but no further expenses of their own.
  select
    fkmemonumber as memonumber,
    fktripid as tripid,
    tripamount,
    null as overtime,
    null as repairing,
    null as lu,
    1 sortkey
  from tblmemotrips
  where fkmemonumber in (select memonumber from memo)
  group by fkmemonumber, fktripid -- one row per memo and trip
  -- -------------------------------------------------------------------------------
  union all
  -- -------------------------------------------------------------------------------
  -- The memos' expenses. They are not reated to particular trips.
  select
    fkmemonumber as memonumber,
    null as tripid,
    null as tripamount,
    sum(case when fkexpensetypeid  = 1 then amount end) as overtime,
    sum(case when fkexpensetypeid  = 2 then amount end) as repairing,
    sum(case when fkexpensetypeid  = 3 then amount end) as lu,
    2 as sortkey
  from tblmemoexpense
  where fkmemonumber in (select memonumber from memo)
  group by fkmemonumber -- one row per memo
  -- -------------------------------------------------------------------------------
) data
order by memonumber, sortkey;

使用以下代码添加额外的验证级别:

"http://news.domain.com/path/..".replace(/(:\/\/\w+\.)/, "://mobile.")

答案 2 :(得分:0)

参考FixMaker对他answer的评论:

  

window.location.href将为您提供完全限定的网址(例如http://news.domain.com/path)。运行上面的代码时,您需要考虑http://前缀

处理请求方案(http / https)的合适正则表达式如下:

function replaceSubdomain(url, subdomain){
    return url.replace(/^(https?:\/\/)(www\.)?([^.])*/, `$1$2${subdomain}`);
}

let url1 = 'https://sub-bar.main.com';
let url2 = 'https://www.sub-bar.main.com';

console.log(replaceSubdomain(url1, 'foobar'));
console.log(replaceSubdomain(url2, 'foobar'));

答案 3 :(得分:-1)

如果您想通过JS将用户发送到新网址 - 请使用document.location = "mobile.domain.com/path/.."

答案 4 :(得分:-2)

您无法替换子域。您可以使用javascript重定向。

<script type="text/javascript">
<!--
window.location = "http://mobile.domain.com/path/to/file.html"
//-->
</script>

答案 5 :(得分:-3)

我尝试使用java脚本但没有运气,对于我的情况,我在.httaccess文件中使用以下代码

RewriteCond %{HTTP_USER_AGENT} "iphone|ipod|android" [NC]
RewriteCond %{HTTP_HOST} !^mobile.domain.com
RewriteRule ^(.*)$ http://mobile.domain.com/ [L,R=302]

它会将“新闻”子域名替换为“移动”子域名。希望它会对任何人有所帮助。