matching regex ignoring whitespace and new lines [duplicate] - javascript

This question already has answers here:
Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms
(10 answers)
Closed 6 years ago.
I have an xml string that looks something like
<parent><child>value</child></parent>
I’m trying to write a regular expression that matches this string. Trick is, sometimes the input has random new lines and spaces in it.
For example:
<parent>
<child>value</child></parent>
I’d like to be able to identify using regex if the input xml has a parent, child and value.
So far I’ve tried:
“<parent>\n\s*<child>value<\/child>”
But this doesn’t seem to match. I don’t understand how to ignore ALL whitespace and ALL new lines in my expression.
SOLUTION:
So when you are working with older systems, in healthcare tools for example you may often need to match xml requests and it's very much possible... through trial and error here's what worked for me:
^<parent>\s*<child>value[\S\s]*

You should not parse XML with a regular expression. Instead use an XML parser, which is available in JavaScript:
var sMyString = '<parent><child>value</child></parent>';
var oDOM = new DOMParser().parseFromString(sMyString, "text/xml");
var txt = oDOM.documentElement.textContent;
console.log(txt);
This parser knows how to deal with white space. Note that white space can also occur in the tags, like <parent >

Related

How to use RegEx with a variable for replacing [duplicate]

This question already has answers here:
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 4 years ago.
My title might not be worded correctly sorry in advance.
I've looked everywhere and I honestly can't seem to figure it out.
I want to use a variable in a RegEx but the solution that i've found and tried to work off of works but it is not flexible for what I need. I can't seem to figure out how to convert it to a RegEx constructor object which I think is my best bet. Any help will be greatly appreciated.
let str = 'haeandviaecy'
let newString = str.replace(/(\w{3})/g, '$1 ').replace(/(^\s+|\s+$)/, '')
//hae and via ecy
replace(/(^\s+|\s+$)/,'') removes any preceding or trailing space from the string - just giving you a heads up
So what this snippet does is go to every third and put a blank space in it. What I want is to make my code more flexible and be able to put a variable to let me choose the number for when it puts a blank space using a variable.
right now it prints out - "hae and via ecy"
for ex. every 4th would be - haea ndvi aecy
I've read you cant put in variables unless it is a RegEx contructor object. When I've tried to convert it doesn't seem to work.
If gather the question correctly, you can use a template literal as parameter passed to RegExp constructor.

Javascript / jQuery: How to convert string to HTML character code [duplicate]

This question already has answers here:
How to convert characters to HTML entities using plain JavaScript
(12 answers)
Closed 8 years ago.
I have a string that contains text including special characters like apostrophes, quotes, ampersand etc. that I would like to convert to HTML character codes, e.g. to show &#39 instead of an apostrophe etc. (as per the following list: http://www.w3.org/MarkUp/html-spec/html-spec_13.html.
I do not necessarily need to convert letters and spaces as long as the above and some other basic characters are encoded correctly.
For a temporary workaround I use simple replaces like str = str.replace("'", "&#39") but would like to avoid this as it only covers certain characters.
I was thinking of encodeURI or encodeURIcomponent but would like to know what is the best and easiest way to achieve this without encoding more than necessary.
Can someone tell what is the best way here if the idea is to avoid any issues with other JS where this is inserted dynamically, esp. thinking of single quotes, double quotes and any other character that could cause issues here (referring to JavaScript / jQuery and English language only) ?
function encodeEntities(value) {
return $('<div />').text(value).html();
}

JS regular expression multi-line [duplicate]

This question already has answers here:
How to split a long regular expression into multiple lines in JavaScript?
(11 answers)
Closed 8 years ago.
This one seems like it has a very simple answer, yet I can't find it anywhere. I have a regular expression that is quite large, how do I put in some line breaks in the expression itself so I don't have to keep scrolling horizontally through the code to see it all?
I don't normally use word-wrap, and the IDE I'm using doesn't even offer it anyway.
A line break in a string would normally be a \ at the end of the line :
var mystring "my string \
is on more \
than one line";
var re = new RegExp(mystring, "gim");
You could use RegExp and .join() to convert and concat a string.
var myRegExp = RegExp(['/^([a-zA-Z0-9_.-])+'
,'#([a-zA-Z0-9_.-])+'
,'\.([a-zA-Z])+([a-zA-Z])+/'].join(''));
The answer has been linked to here as well.
How to split a long regular expression into multiple lines in JavaScript?

RegEx: Extract GET variable from URL [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
RegExp gurus, heed my call!
This is probably super simple, but I've painted myself in a mental corner.
Taking a regular URL, split after the ?, which gives a string like variable=val&interesting=something&notinteresting=somethingelse I want to extract the value of interesting.
The name of the variable I'm interested in can be a substring of another variable.
So the match should be
either beginning of string or "&" character
followed by "interesting="
followed by the string I want to capture
followed by either another "&" or end of string
I tried something along the lines of
[\^&]interesting=(.*)[&$]
but I got nothing...
Update
This is to be run in a Firefox addon on every get request, meaning that jQuery is not available and if possible I would like to avoid the extra string manipulation caused by writing a function.
To me this feels like a generic "extract part of a string with regex" but maybe I'm wrong (RegEx clearly isn't my strong side)
simple solution
var arr = "variable=val&interesting=something&notinteresting=somethingelse".split("&");
for(i in arr) {
var splits = arr[i].split("=");
if(splits[0]=="interesting") alert(splits[1]);
}
also single line match
"variable=val&interesting=something&notinteresting=somethingelse".match(/(?:[&]|^)interesting=((?:[^&]|$)+)/)[1]
function getValue(query)
{
var obj=location.search.slice(1),
array=obj.split('&'),
len=array.length;
for(var k=0;k<len;k++)
{
var elm=array[k].split('=');
if(elm[0]==query)return elm[1];
}
}
This function directly extract the query URL and return the corresponding value if present.
//usage
var get=getValue('interesting');
console.log(get);//something
If you're using the Add-on SDK for Firefox, you can use the url module:
https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/url.html
This is much better than using regex.

javascript replace query string value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
add or update query string parameter
I am trying to replace the page number in the query string no matter what digit is to 1.
query string
index.php?list&page=2&sort=epub
javascript
window.location.href.replace(new RegExp("/page=.*?&/"), "page=1&")
Your code looks almost right; however:
you need to use either new RegExp or the special // regex syntax, but not both.
the replace method doesn't modify the string in-place, it merely returns a modified copy.
rather than .*?, I think it makes more sense to write \d+; more-precise regexes are generally less likely to go awry in cases you haven't thought of.
So, putting it together:
window.location.href = window.location.href.replace(/page=\d+/, "page=1");

Categories