Implementing regex to find span with class inside javascript file - javascript

I am using regex in python to find <span class="dlog"...>Whatever you want to put here</span>, and have been using pythex and crossreferencing regexpal to match the case. However, inside of my javascript file, where I am defining a variable containing the regex, as follows:
var tokenRe = /<span class=\"dlog\"(.*?)\>(.*?)\</span>/;
I am getting an error: SyntaxError: Invalid regular expression: missing /. Thus, I need help trying to implement this regex inside of my javascript file.
Thanks !

You did not escape the slash for </span. You also don't need so many escapes because of the regex literals:
var tokenRe = /<span class="dlog"(.*?)>(.*?)<\/span>/

Related

RegEx to search for a href="something" pattern

I know RegEx should not be used for parsing HTML, but I'm unable to use any other solution, so I'm stuck with this
I got this for URI.js:
/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’&quote]))/ig
However it doesn't work very well, so I wanted to add a prefix that would search only for strings starting with href=
Ended up with something like this (which works in the RegEx tester):
href\=\"\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’&quote]))
But when compiled, it throws "illegal character" error. Not sure if it's the " or = that causes that.
JS code:
matches_temp = result_content.match(href\=\"\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’&quote])));
result_content is taken from the DB.
You need the slashes that say this is a regex, sort of how like quotes say that this value is a string. So .match(regex) should be .match(/regex/). Take a look:
var result_content = 'blah';
var matches_temp = result_content.match(/href\=\"\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’&quote]))/);
console.log(matches_temp[1]);

Regex - syntax error javascript

After reading this Question I am trying to use this regex
/(!<[^>]+>)+/g
Like this
var regex = /(!<[^>]+>)+/g;
new RegExp('myString', regex)
but I get
Uncaught SyntaxError: Invalid flags supplied to RegExp constructor
'/(!<[^>]+>)+/g'
Anybody have idea how to use it right?
new Regexp is used when you have a regular expression in a string and you want to convert it to a regular expression object.
It is awful and you should almost never use it. (The exception is when you are dynamically generating a regex).
You don't need to use it: You created the object using a regular expression literal on the previous line.
To apply the regular expression to a string, use the match method:
var regex = /(!<[^>]+>)+/g;
console.log("myString".match(regex));
console.log("Trivial !<tag> example".match(regex));
console.log("!<tag attribute='>>>'> example".match(regex));
(But applying regex to HTML is usually a terrible idea).
RegExp is used to create regular expression from string
The second attribute should be flags like i , g, m but you're giving a regular expression itself.
The syntax of RegExp is RegExp(regexString,flags)
but you are using
var regex = /(!<[^>]+>)+/g;
new RegExp('myString', regex)
^^^^^^
You can't use the RegExp function to test a string.
if you want to check 'myString' against your regular expression, use regex.test('myString')
if you want to find all matches of the regex in 'myString', use 'myString'.match(regex)

str replace all in Javascript

I am trying to some some urls throught javascript where some replacement of urls needs to be done. I have a textarea with some URLs example given below:
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
Now what i am trying to do is replacing http://mywebsite.com/preview.aspx?mode=desktop&url= with spaces.
I have tried using str.replace() but it is replacing only first occurence of that url.
I have also tried with Global variable g the query i have used is
str_replace(\http://mywebsite.com/preview.aspx?mode=desktop&url=/g,'');
But its not working So can anyone tell me how i can do that ?
I want the output of the textarea like:
http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/post.aspx?id=44&content=4
I believe that your biggest issue is that your regex syntax is incorrect. Try this:
Imagine that var s is equal the the value of your textarea.
s.replace(/http\:\/\/mywebsite\.com\/preview.aspx\?mode\=desktop\&url\=/g, '');
The issue you were having was improper delimiters and unescaped reserved symbols.
Though Javascript has some of its own regex idiosyncrasies, the issues here were related to basic regex, you might find these resources useful:
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
http://regexpal.com
try this.
var string = document.getElementById('textareaidhere');
string.replace(/http:\/\/mywebsite\.com\/preview\.aspxmode=desktop&url=/g, '');
JSFiddle here

RegEx not working in Javascript: SCRIPT5018: Unexpected quantifier

I'm trying to make a RegEx that can single out words from a string but ignore them if they're inside a tag. For example: Even though the searchword is SPAN, do not replace a span tag.
What I have so far is:
(?<![<\/])\bspan\b(?!>)
http://regex101.com/r/vS6yG6
Span obviously is a placeholder. In the script it is generated from a dictionary dynamically.
This is what I'm trying to run:
var reg = new RegExp(the expression, 'gi');
I've escaped the /, so I'm not sure where the problem is.
And this is what I get back: SCRIPT5018: Unexpected quantifier
Any help would be appreciated. I made the Regular Expression with the help of regex101.com.
Try this ...
/>[^<>]*\b(span)\b[^<>]*<?/ig
Like David replied, there is no Negative LookBehind in Javascript, so that's where the problem was.
http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript

Matching regular expression string in Javascript

Does anyone know how to find regular expression string from javascript code?
e.g.
var pattern = /some regular expression/;
Is it possible to to with regular expression :) ?
If I got your question right, and you need a regular expression which would find all the regular expressions in a JavaScript program, then I don't think it is possible. A regular expression in JavaScript does not have to use the // syntax, it can be defined as a string. Even a full-blown JavaScript parser would not be smart enough to detect a regular expression here, for instance:
var re = "abcde";
var regexClass = function() { return RegExp; }
var regex = new regexClass()(re);
So I would give up this idea unless you want to cover only a few very basic cases.
You want a regex to match a regex? Crazy. This might cover the simplest cases.
new RegExp("\/.+\/")
However, I peeked into the Javascript Textmate bundle and is has 2 regex for finding a regex start and end.
begin = '(?<=[=(:]|^|return)\s*(/)(?![/*+{}?])'
end = '(/)[igm]*';
Which you could probably use as inspiration for toward your goal.
Thanks for answers I have found also that it is nearly impossible task to do, but here is my regex which parses source code just fine:
this.mainPattern = new RegExp(//single line comment
"(?://.*$)|"+
//multiline comment
"(/\\*.*?($|\\*/))"+
//single or double quote strings
"|(?:(?:\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|(?:'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'))"+
//regular expression literal in javascript code
"|(?:(?:[/].+[/])[img]?[\\s]?(?=[;]|[,]|[)]))"+
//brackets
"|([{]|[(]|[\[])|([}]|[)]|[\\]])", 'g');

Categories