JavaScript正则表达式似乎忽略了锚点。

huangapple go评论64阅读模式
英文:

javascript regex seems to be ignoring anchor

问题

I understand your request to translate the code portion only. Here is the translated code:

javascript:(function() {window.open(window.location.toString().replace(/^https:\/\/.*?abc.com\//, 'https://dev.abc-1-de.net/'), '_blank');})()

Please note that I've removed the HTML entity (') for single quotes, as it's not necessary in this context.

英文:

I am trying to make a javascript bookmarklet that takes the current page url, modifies the domain to a different one, then opens that in a new tab.

for example, given the url:

https://prod.abc.com/lorum/a-1234/b/c.d/e/1234#fg:hij

I want to run the bookmarklet and have it open

https://dev.abc-1-de.net/lorum/a-1234/b/c.d/e/1234#fg:hij

This is my current code

javascript:(function() {window.open(window.location.toString().replace(/^https:\/\/.*?abc.com\//, 'https://dev.abc-1-de.net/'), '_blank');})() 

When I run

window.location.toString().replace(/^https:\/\/.*?abc.com\//, 'https://dev.abc-1-de.net/')

I get the url I'm expecting. However, when I run the whole thing together, the new tab that opens is directed to

https://prod.abc.com/lorum/a-1234/b/c.d/e/https://dev.abc-1-de.net/lorum/a-1234/b/c.d/e/1234#fg:hij.

To me, this seems like the regex is ignoring my ^ anchor for some reason, but I have no idea why that would be happening only when it is passed into window.open.

答案1

得分: 1

以下是翻译好的内容:

这段代码似乎可以正常运行,就像你说的一样:

var oldUrl = "https://prod.abc.com/lorum/a-1234/b/c.d/e/1234#fg:hij";
var newUrl = oldUrl.replace(/^https:\/\/.*?abc.com\//, 'https://dev.abc-1-de.net/');

console.log(newUrl)

控制台会打印出 https://dev.abc-1-de.net/lorum/a-1234/b/c.d/e/1234#fg:hij,就如你所期望的那样。

当我运行这段代码:

window.open("https://dev.abc-1-de.net/lorum/a-1234/b/c.d/e/1234#fg:hij", "_blank")

我只是被重定向到一个新标签页中的空白窗口。然而,设置 window.location.href 会进行重定向,但它不会在新窗口中打开。

window.location.href = "https://dev.abc-1-de.net/lorum/a-1234/b/c.d/e/1234#fg:hij";

所以你的完整代码如下:

javascript:(function() {
  window.location.href = window.location.toString().replace(/^https:\/\/.*?abc.com\//, 'https://dev.abc-1-de.net/');
}()
英文:

Running this seems to work, as you said:

var oldUrl = "https://prod.abc.com/lorum/a-1234/b/c.d/e/1234#fg:hij";
var newUrl = oldUrl.replace(/^https:\/\/.*?abc.com\//, 'https://dev.abc-1-de.net/');

console.log(newUrl)

The console prints out https://dev.abc-1-de.net/lorum/a-1234/b/c.d/e/1234#fg:hij as you expect it to.

When I run this code:

window.open("https://dev.abc-1-de.net/lorum/a-1234/b/c.d/e/1234#fg:hij", "_blank")

I am just redirected to a blank window in a new tab. However setting window.location.href does redirect, but it does not open in a new window

window.location.href = "https://dev.abc-1-de.net/lorum/a-1234/b/c.d/e/1234#fg:hij";

So your full code would be

javascript:(function() {
  window.location.href = window.location.toString().replace(/^https:\/\/.*?abc.com\//, 'https://dev.abc-1-de.net/');
}() 

答案2

得分: 1

看起来对我来说工作正常。我使用以下代码创建了一个新的书签:

javascript:(function() {window.open(window.location.toString().replace(/^https:\/\/.*?.com\//, 'https://dev.abc-1-de.net/'), '_blank');})()

我从正则表达式中移除了 "abc",现在这个书签将适用于所有域名。它实际上会将域名替换为 "dev.abc-1-de.net"。

英文:

Seems to be working correctly for me.I made a new bookmark with the code javascript:(function() {window.open(window.location.toString().replace(/^https:\/\/.*?.com\//, 'https://dev.abc-1-de.net/'), '_blank');})()

I removed the abc from the regex and now this bookmark will work for all domains. It essentially replaces the the domain with dev.abc-1-de.net

答案3

得分: 1

问题是我提供的替代 URL 中有一个拼写错误,以及 window.open 工作方式中也有错误。我未混淆的代码中提供的替代 URL 中有拼写错误,https//: 而不是 https:

这导致 window.open 函数认为这是一个相对 URL 而不是绝对 URL,因此它附加了它而不是正确地重定向。

英文:

Turns out, the issue was a typo in the replacement url I was giving, as well as in the way that window.open works. The replacement url I was giving in my unobfuscated code had the typo https//: instead of https:.

This made the window.open function think this was a relative url instead of an absolute, and so it was appending it instead of properly redirecting.

答案4

得分: 0

One does not even need a regex because one could utilize URL on that matter. Example ...

const url = new URL("https://prod.abc.com/lorum/a-1234/b/c.d/e/1234#fg:hij");

url.hostname = 'dev.abc-1-de.net';

console.log(
  url.href
);
console.log(
  String(url)
);
英文:

One does not even need a regex because one could utilize URL on that matter. Example ...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const url =
  new URL(&quot;https://prod.abc.com/lorum/a-1234/b/c.d/e/1234#fg:hij&quot;);

url.hostname = &#39;dev.abc-1-de.net&#39;;

console.log(
  url.href
);
console.log(
  String(url)
);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 02:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定