473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

javascript regexp

Hi,

i have script with pattern (href=['"]?(.*)([#]{1}[^'"<>]+)['"]?),
where i match any occurence of url and replace hyperlink so i have
only anchor in it.

var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.innerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.replace(regExp, 'href="$2"');

All works great, but I dont want to change URL's which containt word
'item_id'

How to do that? :D

Thx for help

Mar 26 '07 #1
8 3565
On Mar 26, 5:08 am, "reflex" <reflexa...@gmail.comwrote:
var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.innerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.replace(regExp, 'href="$2"');

All works great, but I dont want to change URL's which containt word
'item_id'
var regExp = /href=(['"])?(.*)([#]{1}[^'"<>]+)\1/ig;
var links=document.links;
for(var i=0;i<links.length;i++){
if(!links[i].href.indexOf("item_id"))
links[i].href.replace(regExp, 'href="$3"');
}

Mar 26 '07 #2

scripts.contact napsal:
On Mar 26, 5:08 am, "reflex" <reflexa...@gmail.comwrote:
var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.innerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.replace(regExp, 'href="$2"');

All works great, but I dont want to change URL's which containt word
'item_id'

var regExp = /href=(['"])?(.*)([#]{1}[^'"<>]+)\1/ig;
var links=document.links;
for(var i=0;i<links.length;i++){
if(!links[i].href.indexOf("item_id"))
links[i].href.replace(regExp, 'href="$3"');
}
Very nice, but I cant use DOM for this. I am changing content of
string variable.

Mar 26 '07 #3
scripts.contact wrote on 26 mrt 2007 in comp.lang.javascript:
On Mar 26, 5:08 am, "reflex" <reflexa...@gmail.comwrote:
>var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.innerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.replace(regExp, 'href="$2"');

All works great, but I dont want to change URL's which containt word
'item_id'

var regExp = /href=(['"])?(.*)([#]{1}[^'"<>]+)\1/ig;
var regExp = /href\s*=\s*(['"])?([^#]*)(#[^'"<>]+)\1/ig;

I think.
var links=document.links;
for(var i=0;i<links.length;i++){
if(!links[i].href.indexOf("item_id"))
if(!/item_id/.test(links[i].href))

links[i].href.replace(regExp, 'href="$3"');
links[i].href = links[i].href.replace(regExp, 'href="$3"');

>}
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 26 '07 #4
reflex wrote on 26 mrt 2007 in comp.lang.javascript:
>
scripts.contact napsal:
>On Mar 26, 5:08 am, "reflex" <reflexa...@gmail.comwrote:
var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.innerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.replace(regExp, 'href="$2"');

All works great, but I dont want to change URL's which containt word
'item_id'

var regExp = /href=(['"])?(.*)([#]{1}[^'"<>]+)\1/ig;
var links=document.links;
for(var i=0;i<links.length;i++){
if(!links[i].href.indexOf("item_id"))
links[i].href.replace(regExp, 'href="$3"');
}

Very nice, but I cant use DOM for this. I am changing content of
string variable.
Why not?

Simply reenstate the old innerHTML after use:

var saveHTML = document.body.innerHTML;

// Do the above DOM things

var yourResultHTMLstring = document.body.innerHTML;

var document.body.innerHTML = saveHTML;
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 26 '07 #5
On Mar 26, 6:01 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
scripts.contact wrote on 26 mrt 2007 in comp.lang.javascript:
On Mar 26, 5:08 am, "reflex" <reflexa...@gmail.comwrote:
var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;>
All works great, but I dont want to change URL's which containt word
'item_id'
var regExp = /href=(['"])?(.*)([#]{1}[^'"<>]+)\1/ig;

var regExp = /href\s*=\s*(['"])?([^#]*)(#[^'"<>]+)\1/ig;
or
var regExp = /href\s*=\s*(['"])?[^#]*(#[^>\1 ]+)/ig;

and then use 2nd match.
var links=document.links;
for(var i=0;i<links.length;i++){
if(!links[i].href.indexOf("item_id"))

if(!/item_id/.test(links[i].href))
or
if(links[i].href.indexOf("item_id")==-1)
Mar 26 '07 #6
If you have something like:

var myString = 'Blablabla <a href="index.html?
item_id=44456465#To_the_anchor">looks nice</aBlablabal';

var links = myString.links;

doesnt work, bcs myString is not an element.

Sry for my bad explanation :]

Mar 26 '07 #7
reflex wrote on 26 mrt 2007 in comp.lang.javascript:
If you have something like:

var myString = 'Blablabla <a href="index.html?
item_id=44456465#To_the_anchor">looks nice</aBlablabal';

var links = myString.links;

doesnt work, bcs myString is not an element.

Sry for my bad explanation :]
Where are you responding on?

[please always quote on usenet]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 26 '07 #8
Where are you responding on?

To every one.

Try something like this:

var testText = 'asdjlkajsdkljlasdjlakl <a href="adasda.html">ada</a>
asdadadada <a href="adada.php?item_id=4454#Anchor">sdada</a>';

var regExp = /href=(['"])?(.*)([#]{1}[^'"<>]+)\1/ig;
var links=testText.links;
for(var i=0;i<links.length;i++){
if(!links[i].href.indexOf("item_id"))
links[i].href.replace(regExp, 'href="$3"');
}

The problem is, that function "links" works only with 'document', but
i have to use 'text' (string)
http://msdn.microsoft.com/workshop/a...ions/links.asp

I am sorry for my earlier bad explanation
var wholeContent = document.body.innerHTML; //doenst look like this
I only make it for example

thats my mistake, it should looks like
var wholeContent = 'asdjlkajsdkljlasdjlakl <a href="adasda.html">ada</
aasdadadada <a href="adada.php?item_id=4454#Anchor">sdada</a>';

Thx for patience :]

Mar 26 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Davide R. | last post by:
Ciao a tutti, vi spiego il mio problema Ho una pagina HTML che referenzia un CSS esterno. Ho alcuni elementi HTML che appartengolo ad una classe (chiamiamola "class1"). Avrei la necessitą,...
7
by: Richard Trahan | last post by:
I need a javascript function to hex-encode a plus sign so I can pass the plus sign as an argument in a GET request. escape() and encodeURI() don't do it (and probably shouldn't, because '+' is a...
2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
7
by: Leif902 | last post by:
After much searching of google, the closest I can find is highlighting search terms... however, this is not what I wanted to do. Does anyone know how to parse through a specific element (lets say...
16
by: shyamg | last post by:
Hi, this is my javascript validating the fields in mozilla FF but its working and validating only one field. how to write the and how to works the script .................. function...
2
by: Nathan Sokalski | last post by:
I have the following script that I am using to test some JavaScript RegExp code: function RE() { var testing1=new RegExp("*"); var testing2=new RegExp("{0,}"); var testing3=new RegExp("+");...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.