JavaScript Regex expression to extract data from html comment - javascript

There is a html comment with an Id that I need to extract. The comment is on a div, which is not hard to get using the JQuery $ operator. But the correct RegEx string I need I have not been able to figure out. This is the comment:
<!-- sid=FFKK12H1 -->
And I need a JS variable that has the string "FFKK12H1" assigned to. What is the correct syntax/expression to use? thanks!
EDIT:
I forgot a very important piece of information: The code needs to work on IE7. Unfortunately this is the browser my company allows us to use, and none of the proposed solutions work there so far. Any other thoughs?

The regular expression would be: /<!-- sid=(.+?) -->/i:
var str = '<!-- sid=FFKK12H1 -->';
console.log(str.match(/<!-- sid=(.+?) -->/i)[1]);

var content = $('#comment-containg-div').html();
var regex = /<!--\s*sid=([\x00-\x7F]+)\s*-->/;
var matches = regex.exec(content);
console.log(matches);
The regex here is a amalgamted answer that includes all of the suggestions that other people on the page have made, it seems like it would be the safest to use.

var my_id = my_string.replace(/.*<!-- sid=(.*) -->.*/gi, '$1');
Example
http://jsfiddle.net/YTdKQ/

Related

Replace params in javascript

I tried a lot to replace the query parammeter using Javascript. But its not working. Can you please share any solutions to replace the parameter
Below is the example
console.log("www.test.com?x=a".replace(new RegExp(`${"x=a"}&?`),''));
the output i am getting is www.test.com? . Is there any way to replace ? and to get only www.test.com.
If you want to remove whatever comes from the question mark including it, try this instead:
console.log("www.test.com?x=a".split("?")[0]);
That way you get only what's before the question mark.
I hope that helps you out.
You can remove all query strings using the following regex:
\?(.*)
const url = "www.test.com?x=1&b=2"
console.log(url.replace(/\?(.*)/, ''));
You could brutally replace the '?x=a' string with the JavaScript replace function or, even better, you could split the string in two (based on the index of ?) with the JavaScript split function and take the first part, e.g.:
let str = 'www.test.com?x=a';
console.log(str.replace('?x=a', ''));
console.log(str.split('?')[0]);

Cant get the correct regex

It drives me crazy to get the correct regex, can any one help, much appreciated.
Source String:
<checklist><checklist class="ng-scope">it can be any content but no more "checklist tag" pair inside</checklist></checklist>
<checklist><checklist class="ng-scope">it can be any content but no more "checklist tag" pair inside</checklist></checklist>
Result string needed :
<checklist></checklist>
<checklist></checklist>
Basically I need to get rid of the content in between pair (no class attribute).
I tried regex something like this
"/[^(.?)[^]*/g" using phone editing , if you can see this correctly , please see the regex I included in the comment
it didn't work, i am fairly new to regex
The following code snippet can repeat multiple times in the source string:
<checklist><checklist class="ng-scope">it can be any content but no more "checklist tag" pair inside</checklist></checklist>
If you insist on a solution with regular expressions, you could do sth. like:
var string = '<checklist><checklist class="ng-scope">it can be any content but no more "checklist tag" pair inside</checklist></checklist>';
var regex = /<checklist\s+[^>]+>.*?<\/checklist>/gi;
// that is, look for a checklist tag with additional attributes
// match everything up to a new closing tag (non-greedy)
// followed by a closing tag
var strippedString = string.replace(regex, '');
alert(strippedString);
See a JS fiddle here and a regex101 demo here.
EDIT: Added /g as #Atri pointed out.
Otherwise, consider using either document.getElementById or some other DOM function.

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 extraction of one letter inside html chunk

Hi need to extract ONE letter from a string.
The string i have is a big block of html, but the part where i need to search in is this text:
Vahvistustunnus M :
And I need to get the M inside the nbsp's
So, who is the quickest regex-guru out there? :)
Ok, according to this page in the molybdenum api docs, the results will be all of the groups concatenated together. Given that you just want the char between the two 's then it's not good enough to match the whole thing and then pull out the group. Instead you'll need to do something like this:
(?<=Vahvistustunnus )[a-zA-Z](?= )
Warning
This might not work for you because lookbehinds (?<=pattern) are not available in all regex flavors. Specifically, i think that because molybdenum is a firefox extension, then it's likely using ECMA (javascript) regex flavor. And ECMA doesn't support lookbehinds.
If that's the case, then i'm gonna have to ask someone else to answer your question as my regex ninja (amateur) skills don't go much further than that. If you were using the regex in javascript code, then there are ways around this limitation, but based on your description, it sounds like you have to solve this problem with nothing but a raw regex?
Looks like it uses JavaScript and if so
var str = "Vahvistustunnus M :";
var patt = "Vahvistustunnus ([A-Z]) :";
var result = str.match(patt)[1];
should work.

RegEx to replace html entities

all. I'm looking for a way to replace the bullet character in Greasemonkey. I assume a Regular Expression will do the trick, but I'm not as well-versed in it as many of you.
For example, "SampleSite.com • Page Title" becoming "SampleSite.com Page Title". The issue is that the character has already been parsed by the time Greasemonkey has gotten to it, and I don't know how to make it recognize the symbol.
I've tried these so far, but they haven't worked:
newTitle = document.title.replace(/•/g, "");
newTitle = document.title.replace("•", ""); //just for grins, but didn't work anyway
You can do something like this, if Malvolio's solution isn't working
newTitle = document.title.replace(/\&bull\;/g, '');
newTitle = newTitle.replace(/([^a-zA-Z0-9-_\s\/\\\(\)\'\"\&\+\.]+)/g, '');
document.title = document.title.replace(/•/g, "");
works for me.
HTML entities defined by code between & and ; replace &#183; or &middot; (probably in your case) based on page encoding. Better encode html before using RegEx to replace.

Categories