Let's say the structure of the page URL is http://string.static.com/*
string may vary but will only consist of any combination of alphanumeric characters and dashes.
static.com is self-explanatory.
* could be anyting containing letters, numbers, /,:,? and similar characters, so basically there can be directories and parameters as well.
What I'm trying to achieve is this:
The bookmarklet should replace everyting after .com/... so the url becomes http://string.static.com/archive
I've created two (very similar) JS bookmarklets so far but I can't wrap my head around this one. It should be fairly simple but I haven't had any luck. Thanks in advance.
Related
I do know good HTML and some PHP, but not at all javascript, so I need some help with this question please. I have a wordpress site the uses math equations together with Simple Matjax plugin for rendering the math from latex. There are many worpress plugins for latex some of them faster, some of them slower.
I tried to find a faster plugin to replace Simple Mathjax, but every other plugin use a different math "quotation" than that I am using $...$
So I need a javascript that either replaces the $...$ with $$...$$ or first $ with $latex (or whatever start and end sequence I might need for a specific plugin). I guess this is simple, but as I was saying, I do not know javascript at all. Thank you.
PS. There is one replacement WP Quick Latex but when I install it it block the access to my homepage (all other pages are rendered and displayed, except my homepage, page 2, page 3,...that contain excertpts). Any suggestions? Edit: Quicklatex uses shortcodes [latexpage]. If one enables it sitewide on the homepage it has to work on all equations, Thus the wating time for loading the homepage increases significantly.
The javascript function for string replacement is
replace("string to replace", "new string")
I think you should do something like this:
var string = "$"; // <-- this is where you enter the string you want to replace
var new_replaced_string = string.replace("$", "$latex");
Hope this helps.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am looking for a Javascript regex to find opening tags at the end of a string. I want to find all opening tags (with no other regular text in between) followed only by other opening tags or no string till the end.
Example String: <b>sds</b>This is a<u> test.<br><b><span class="test">;
Desired result <b><span class="test">
EDIT: To clarify: I do have to handle a string that contains partial html content. It is a chunk of not in itself valid html. Therefor i cannot operate on dom nodes. Because of the fact that i am in a very special environment i do not have nested tags nor invalid tags - just regular inline tags (and even there u,b,i,span,sup,sup,img,br only).
I am open to solutions not including a regex, but i am not able to work with dom nodes here.
It's not only that you should - you can't. Regex can capture groups and look for them again later in the string, so in theory you could find matching HTML tags. Finding which ones DON'T have a match - or trying to find pairs of tags in a non-conforming HTML code - seems rather complex. You also need to ignore all attributes and attribute names. This is quickly getting out of hand.
But where Regex clearly cannot solve this for you is when you have multiple of the same tags, and when you might have invalid crossing tags like <b><i>Sample text</b></i>. I don't think it's possible to work out every exception case, but I think it's safe to say that if it's possible to do it in a single regex, it would take a lot of time to run and forever to write.
Plus if you need to detect auto-closing elements written without the ending />, that makes things even less doable considering new elements are added to HTML every now and then and your code wouldn't be able to deal with them.
Your best bet is to use DOM manipulation if you want to fix something. Or create good HTML in the first place.
EDIT: Even well formed documents are impossible to work with as there's no way to find which tag matches which when there are multiple instances of the same tag in a row. Example: http://regexr.com/3c2mb
The pattern you are looking for is:
(?:<(?!(?:[hb]r|img|link|other_self_closing_tags)\b)\w+\b[^">]*(?:"[^"]*"[^">]*)*>)+$
details:
(?:
< # start of the tag
(?! # lookahead assertion (not followed by)
(?:[hb]r|img|link|other_self_closing_tags)
\b # word boundary
)
\w+ \b # tag name
[^">]* # all that is not a " or a >
(?:"[^"]*"[^">]*)* # quoted substrings (to deal with attributes)
>
)+
$ # end of the string
I am pretty new to this so please take it easy on me.
I have a javascript bookmark that I use to look up tracking number for parcels on Purolators website (Canadian shipping company). What I want it to do is to take the input (tracking number) and remove any spaces in it before opening the URL. The tracking site is stupid and uses spaces as delimiters for new tracking numbers.
Also, as a bonus, can this be made to open in a new tab?
javascript:var%20trackID%20=%20escape(prompt('Enter%20Tracking%20#'));window.location='https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin='%20+%20trackID;
I have tried adding trackID%20=%20trackID.replace(/\s+/g,%20''); before the window.location but no luck.
Any ideas?
You don't need to use escape in this scenario. This is actually replacing your spaces with %20 special characters which is why your replace wasn't working
Check http://www.w3schools.com/jsref/jsref_escape.asp for more info.
Your corrected code should be something like
javascript:var trackID=prompt('Enter%20Tracking%20#').replace(/ /g,"");window.location='https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin='+trackID;
I have a few strings and I would like to insert some line breaks into them at certain points.
I figured out a few of the logistics but as a whole I can't seem to crack this problem, probably because I have limited experience with regex.
Basically I have a long string of XML tags that is all on one line. I want to add line breaks at certain points to get the data more formatted and looking nice. I am using CodeMirror to display this data on a webpage but for some reason its all on line #1.
So I need to go from something like this:
<Sample><Name></Name><PhoneNumber><AreaCode></AreaCode><Number></Number></PhoneNumber></Sample>
To something like this:
<Sample>
<Name></Name>
<PhoneNumber>
<AreaCode></AreaCode>
<Number></Number>
</PhoneNumber>
</Sample>
CodeMirror will take care of the rest of the formatting all I need to do is insert the line breaks in the right spot using regex or a loop of some sort. The Tags will or can change so I am guessing regex has to be used.
I have had success inserting line breaks with \n and 
 but can't seem to get regex to detect the proper locations.
Any help would be greatly appreciated. Thanks.
UPDATE
I overlooked this but the brackets are in fact being sent as < and >
So example tag would look like:
<PhoneNumber>
or
</PhoneNumber>
So basically need to insert a \n after every > that is a closing tag or a beginning tag that contains children tags.
There be dragons here.
I'd like to point you to a very similar question answered awhile ago that does a good job of explaining why you should NOT try to parse XML yourself unless you REALLY know what you're doing.
Use an XML deserializer if you want to get nice line breaks and that sort of thing.
Try this regex pattern:
>\s*<(?!/)
Replacement string : >\n<
UPDATE:
>\s*<(?!/)
Currently I have a regular expression that will find all URL's within a block of html. It looks like this:
elementHTML.match(/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/ig);
When a URL is detected it is replaced with something that looks like:
<div data="URL_THAT_WAS_DETECTED">Information about that url</div>
The data attribute is custom added.
How can I continue to look for URL's without picking up the previously detected URL?
Ideally, I would like either ignore URL's that are in quotes or possibly html tags but I'm open to suggestions.
Any help is greatly appreciated, Thanks!
This regex will do it:
/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+(?![^\s]*?")([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/ig
I've added a negative lookahead for double quotes " characters.
Live Demo