Regex CSS full path - javascript

I would like to regex the fullpath of the css, for example I have this:
<link rel="stylesheet" href="../assets/myCssFile.css"/>
I would like to regex: /assets/myCssFile.css
What I tried is this: /(?:href)=("|').*?([\w.]+\.(?:css))\1/gi
but this returns me this: href="../assets/myCssFile.css"
Can someone help me out with the regex.
BTW: It is an response text of an ajax request which returns me a string of the html page

href=\"(.*\.css)"
This will return "../assets/myCssFile.css" in a capture group.
https://regex101.com/r/a44Axz/1
All it is doing is saying:
I am only interested in text within quotes that immediately follows an "href" and only if it is a ".css" file.

Late, but maybe it will help someone. :)
Another regex that matches css paths:
href[ \t]{0,}=[ \t]{0,}"(.{1,}\.css)"
This also matches cases when between href and = exists tabs or spaces or between = and css filename.
Also, if you want to match css and javascript filenames in one regex you can use something like this:
href[ \t]{0,}=[ \t]{0,}"(.{1,}\.css)"|src[ \t]{0,}=[ \t]{0,}"(.{1,}\.js)"
They are combined by or operator |.

Related

Regex in an XML string

I would like to get the "MyDocType" string/value of the following string using regex. Yes, it is an XML node, but I really need to get it using regex! :)
<cmis:propertyString propertyDefinitionId="ctp:DocType" displayName="document type" queryName="ctp:DocType"><cmis:value>MyDocType</cmis:value></cmis:propertyString>
Please note the I may have other matches. I want all. Also, there are other tags in the string, but the following rules may be sufficient to get the string:
starts with:
it has some text between
but the value is between the <cmis:value> tags.
I can't just look at the <cmis:value> tags because there are occurrences of it in other places that I don't want to match.
Finally, I need to get the link after the href=" text. I can also have many.
<link rel="self" href="http://localhost:8080/alfresco/s/cmis/s/workspace:SpacesStore/i/6c0dc826-179b-4ed6-9c3a-300f45d6556b"/>
thank you very much!
Miguel

Regular Expression matching extra unwanted content

I'm trying to get a parameter stored in a html comment using regex. However when I execute the expression it return the widest string possible and not all the possible matches.
So I have some content that might include this string:
<!--url:/new--><!--title:My Title-->
I use the following simply expression to get the url I need:
/<!--url:(.*)-->/
The issue I have is that the result match part of the title which is of course valid but not what I was looking for
["<!--url:/new--><!--title:My Title-->", "/new--><!--title:My Title"]
There is workarounds I can use like making sure there is a line break after each parameter line but I prefer to have a solid regex and also of course understand what I missing out.
PS: Please comment if you come up with a better title.
Make the regex non-greedy:
/<!--url:(.*?)-->/
You can test this regex by clicking here:
Regex101

Regular expression in javascript

I have two strings
http://dfdkdlkdkldkldkldl.jpg (it is image src which is staring with http and ending with image)
http://fflffllkfl
now i want to replace http:// with sometext only on those url which having image
what i tried (http(s?):)|([/|.|\w|\s])*.(:jpg|gif|png)
it replacing http of both string.
Any body can help
Thanks
Here is a valid regex:
(https?:)\/\/.*(jpg|gif|png)
That will only match the "image" url. You can play around with it online here: http://regex101.com/
Edit
Basically, your Regex was not only invalid, but too convoluted. You had a sub-group for the "s" on "https", which wasn't needed according to the problem you proposed. Also, you had the OR operand trying to separate the http part and the rest of the url, which made no sense..., lastly, you were grouping the text between ":" and the dot ".", which again, according to your problem description it wasn't needed.
Hope that helps
Edit 2
Ok, so I don't know how exactly the replacement is being done, you're not using your code, you're using a page for that, but here is how you should be doing it:
"http://dfdkdlkdkldkldkldl.jpg".replace(/(https?:)(\/\/.*)(jpg|gif|png)/, "lalala$2$3")
Note that the RegEx changed to: (https?:)(\/\/.*)(jpg|gif|png)
If you try it with the other url: "http://fflffllkfl".replace(/(https?:)(\/\/.*)(jpg|gif|png)/, "lala$2$3") it won't replace anything.
Try this:
myString.replace(/(https?:\/\/)(.*\.jpg|gif|png)/, "some string $2");

javascript regex replace some words with links, but not within existing links

Trying to replace certain words in HTML pages with the same word but as a URL linking to that resource.
For example, replace the word 'MySQL' with MySQL
Using the JS replace function with regex, and it's doing the replacing just fine.
BUT it's also replacing words that are already part of URLs... which is the problem.
For the MySQL example, it's replacing BOTH the "MySQL" text that's already linked, AND the URL leading to mysql.com, so breaking the already existing link.
Is there a way to update the inline regex (in the .replace call) to NOT do replacing in existing links, i.e. elements?
Here's the replace code:
var NewHTML = OriginalHTML
.replace(/\bJavaScript\b/gi, "$&")
.replace(/\bMySQL\b/gi, "$&")
;
Here's the full sample code (tried to paste it inline but wasn't looking right with the backticks):
http://pastie.org/private/v4l2s2c42aqduqlopurpw
Went through the JS regexp reference (here), and tried various other permutations in the regex matching, like the following, but all that does it make it not match ANY words on the page...
.replace(/\b(\<a\>*!\>)JavaScript\b/i,xxxxx
The following regex DOES prevent the match from happening wherever the word is literally touching a slash or a dash... but that's not the solution (and it does not fix the mysql example above):
.replace(/\b(?!\>)(?!\-)(?!\/)MySQL\b(?!\-)(?!\/)/gi, "$&")`
I've read through the related threads on stackoverflow and elsewhere, but can't seem to find this particular scenario, not in JavaScript anyway.
Any help would be greatly appreciated. :-)
Thanks!
You could change your regex to exclude keywords that precede the end anchor tag, </a>:
.replace(/\bMySQL\b(?![^<]*?<\/a>)/gi, "$&")
See jsfiddle for example.
A negative lookahead should be sufficient:
.replace(/\bMySQL(?!\.com)\b/gi, "$&")

How do you to create a RegEx to match a word only if another word appears before it in the sentence?

I need to create a RegEx (that works in Eclipse's search/replace) that will match a group of letters only if a different match occurs anywhere prior in the same sentence.
Specifically, I need to match .css only if it occurs in the same line that includes jsSrc.
Eclipse doesn't support indefinite lookbehinds.
Example:
<link rel="stylesheet" type="text/css" href="<% { out.print( jsSrc( request, "/builder/builder.css" />
Attempted regEx:
\.css(?<=jsSrc)
Does not produce a match of .css
You used the lookbehind ?<= the wrong way, here is the sample from book Mastering Regular Expression: provide you have string see Jeffs book, and you replace (?<=\b Jeff)(?=s\b) with ', you will get see Jeff's book
In your case, there's no need to use lookbehind:
jsSrc.*\.css
works for me.
Maybe you want replace only .css part with XXX, then you can do it like this:
In Find: field, input (jsSrc.*)\.css and in Replace with: field , input \1XXX
You could go with a really straight-to-the-point regex:
jsSrc.*\.css
This will match any line that contains jsSrc before .css with text inbetween both.
Though not specified in the question, if you want to match the file-path within the quotes in the line, you could use something like:
jsSrc.*"([^"]*\.css)"

Categories

Resources