473,799 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.i nnerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.re place(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 3585
On Mar 26, 5:08 am, "reflex" <reflexa...@gma il.comwrote:
var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.i nnerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.re place(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.len gth;i++){
if(!links[i].href.indexOf(" item_id"))
links[i].href.replace(r egExp, 'href="$3"');
}

Mar 26 '07 #2

scripts.contact napsal:
On Mar 26, 5:08 am, "reflex" <reflexa...@gma il.comwrote:
var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.i nnerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.re place(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.len gth;i++){
if(!links[i].href.indexOf(" item_id"))
links[i].href.replace(r egExp, '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.javas cript:
On Mar 26, 5:08 am, "reflex" <reflexa...@gma il.comwrote:
>var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.i nnerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.re place(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.len gth;i++){
if(!links[i].href.indexOf(" item_id"))
if(!/item_id/.test(links[i].href))

links[i].href.replace(r egExp, 'href="$3"');
links[i].href = links[i].href.replace(r egExp, '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.javas cript:
>
scripts.contact napsal:
>On Mar 26, 5:08 am, "reflex" <reflexa...@gma il.comwrote:
var regExp = /href=['"]?(.*)([#]{1}[^'"<>]+)['"]?/ig;
var wholeContent = document.body.i nnerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.re place(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.len gth;i++){
if(!links[i].href.indexOf(" item_id"))
links[i].href.replace(r egExp, '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.i nnerHTML;

// Do the above DOM things

var yourResultHTMLs tring = document.body.i nnerHTML;

var document.body.i nnerHTML = 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.net wrote:
scripts.contact wrote on 26 mrt 2007 in comp.lang.javas cript:
On Mar 26, 5:08 am, "reflex" <reflexa...@gma il.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.len gth;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.htm l?
item_id=4445646 5#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.javas cript:
If you have something like:

var myString = 'Blablabla <a href="index.htm l?
item_id=4445646 5#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 = 'asdjlkajsdkljl asdjlakl <a href="adasda.ht ml">ada</a>
asdadadada <a href="adada.php ?item_id=4454#A nchor">sdada</a>';

var regExp = /href=(['"])?(.*)([#]{1}[^'"<>]+)\1/ig;
var links=testText. links;
for(var i=0;i<links.len gth;i++){
if(!links[i].href.indexOf(" item_id"))
links[i].href.replace(r egExp, '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.i nnerHTML; //doenst look like this
I only make it for example

thats my mistake, it should looks like
var wholeContent = 'asdjlkajsdkljl asdjlakl <a href="adasda.ht ml">ada</
aasdadadada <a href="adada.php ?item_id=4454#A nchor">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
2231
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à, alla pressione di un tasto di lanciare un javascript che mi cambia un attributo della classe "class1" (per esempio 'font-size', ma potrebbe essere qualsiasi altro attributo). Teoricamente potrei fare un loop in tutti gli elementi della pagina e
7
9572
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 valid character in a URI). I could write a regexp, but the environment I'm in won't allow replace(). I could write something cumbersome using indexOf, but if there's something easier, like a builtin function that encodes every character in the...
2
1876
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 parse the new language, Delphi/Pascal. Source to the highlighter is avail here: http://www.dreamprojections.com/SyntaxHighlighter/Default.aspx ********************************************** COMMENTS
24
6342
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 have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
2
14679
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 response. A very strange effect (and I can't find where I've done something wrong) is that regexp matches in this function fail on every second call.
7
2341
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 the innerHTML of a div) and add tags to change the styles of several keywords? For instance, I might want the words "and", "or" and "xor" to be bold and the words "c_white", "c_red" and "c_orange" to appear as maroon... (it's for a syntax...
16
1964
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 Test(){
2
1118
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("+"); var testing4=new RegExp("{1,}"); window.alert(testing1.test("ab")+"\n"+testing2.test("ab")+"\n"+testing3.test("ab")+"\n"+testing4.test("ab"));}When this script is run, the window.alert contains the following results:truetruefalsefalseThe the first...
2
9042
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 figure this problem out. Any suggestions? thanks Joel
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.