[英]javascript preg_match of innerHTML
I'm trying to get a link from a page between 我正在尝试从之间的页面获取链接
url: '
and ',
url: '
和',
I have tried a few different solutions found via google but the closest I have got is this 我尝试了一些通过google找到的不同解决方案,但我最近的是
function pregmatch() {
var re = new RegExp("url: '(.*)',", "g"),// the regex
txt = 'some text'; // the text on page to be replaced by url
newtxt = txt.replace(txt,re); // replace text with found url
document.body.innerHTML = document.body.innerHTML.replace(txt,newtxt);
}
pregmatch();
but this simply replaces the some text
with the regex /url: '(.*)',/g
and not the url which what is needed. 但这只是用正则表达式
/url: '(.*)',/g
代替some text
,而不是所需的url。
I also tried newtxt = txt.replace(txt,re[0]);
我也尝试过
newtxt = txt.replace(txt,re[0]);
but that came back as undefined, I am a JS novice so any help is appreciated 但这回来是不确定的,我是JS新手,因此对您有所帮助
UPDATE: 更新:
Ok I think some miss-understand what I'm trying to do so will try again 好吧,我想我会做一些误会,然后再试一次
In the pages source there is a section: 在页面源中有一个部分:
url: 'http://somedomain.com/b3a4f5d2b725a8d',
On the page itself there is some text hello world
which is situated perfectly for where I want to add the url on page, so i want to grab the url from the source and replace the hello world
with the url. 在页面本身上,有一个文本
hello world
,它恰好位于我要在页面上添加url的位置,因此我想从源中获取url并用url替换hello world
。
ie var txt = 'some text'
is not the string from where the url is but infact just text on page that will be replaced with the found url. 即
var txt = 'some text'
不是URL所在的字符串,而实际上只是页面上将被找到的URL替换的文本。
The syntax is: 语法为:
string.replace(regex, replacement);
Thus: 从而:
txt.replace(re, somereplacement);
But since you want to get a url, you might be better off using .match
: 但是,由于您要获取网址,因此最好使用
.match
:
url = txt.match(re)[1];
And your re
would be better with: 和你
re
会更好:
var re = new RegExp("url: '(.*?)',", "g");
This prevents matching more than necessary. 这样可以防止不必要的匹配。
You are replacing matches of your regex in string txt
with your regular expression. 您正在用正则表达式替换字符串
txt
中的正则表达式匹配项。
Correct usage is: 正确用法是:
var newtxt = txt.replace(re, "something_to_replace_with");
to get all matches: 获得所有匹配项:
var matches = txt.match(re);
Without explicitly defining a regex object: 没有显式定义正则表达式对象:
var reg = /url: '(.*?)',/;
var txt = "url: 'some text',";
if (reg.test(txt))
alert(txt.match(reg)[1]);
Also be aware that 还请注意
var txt = 'some text';
Will not match a regular expression looking for single quotes, because the single quotes used above are only used to define the variable as a string -- they don't actually exist as matchable text IN the string itself. 不会与寻找单引号的正则表达式匹配,因为上面使用的单引号仅用于将变量定义为字符串-实际上,它们在字符串本身中不作为可匹配的文本存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.