JS regular expression multi-line [duplicate] - javascript

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?

Related

matching regex ignoring whitespace and new lines [duplicate]

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 >

Pattern check returning false javascript/typescript [duplicate]

This question already has answers here:
JavaScript RegExp objects
(5 answers)
Closed 6 years ago.
I am trying to validate if a string I entered matches the date format 'MM/yyyy'
Below is a sample of the code I am using for the same:
var date='05/2016'
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$');
patt.test(date);
However the above code is returning false.
I tried running it with the regex checker:
https://regex101.com/
The pattern seems to be working fine.
Could someone please let me know what is missing.
https://jsfiddle.net/ymj6o8La/
You have to escape the string that is passed to RegExp (the backslashes).
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
Even better, in your case, it's not dynamic, so you should use the literal RegExp instead
var patt = /^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$/
You should escape your backslashes. To represent \d or even \ you should another backslash behind it (e.g: \\) :
var date = '05/2016'
var patt = new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
console.log(patt.test(date));
Try using a pattern like this
patt= /^((0[1-9])|(1[0-2]))\/(\d{4})$/;

Replace values between parentheses using Javascript and Regex [duplicate]

This question already has answers here:
How can I replace a string in parentheses using a regex?
(4 answers)
Closed 7 years ago.
I need to replace the text between two parentheses using Regex in Javascript. For example:
var x = "I need to go (now)";
I need to replace 'now' with 'tomorrow'. I tried this, but it didn't work:
x.replace(/\(now)\b/g, 'tomorrow');
"I need to know (now)".replace(/\(now\)/g, 'tomorrow');
You don't need the \b and you need to escape the second ).

String-variables having many lines in js [duplicate]

This question already has answers here:
Creating multiline strings in JavaScript
(43 answers)
Closed 7 years ago.
EDIT OK, I've realised there is already an answer answering this question with NO: Creating multiline strings in JavaScript
Thanks for all escaping/concatenating answers, but it is not what I needed.
END EDIT
In Python it is possible to define string variables having many lines by the notation
"""
many
many lines
"""
Is there something like this in JavaScript?
I think you could use it like this:
var str = 'many\n' +
'many lines';
Although such a thing does not exist in Javascript there is a way around it. Instead of the occasional """ """, you can add a \ at the end of each line. That in turn will create a "Multi-line string". Here is an example:
var myString = "This is \
my multi \
line string";
EDIT
I thought I should also point this out. Another way of accomplishing this is to concatenate together the strings, like so:
var myString = "This is" +
"my multi" +
"line string";

Replace string including asterisk in Javascript [duplicate]

This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 7 years ago.
I am trying to replace the same string *a*a consistently with *a.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a being replaced with xyz
* is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/
An asterisk is a special regex character.
You just have to escape it like this: \*a in place of *a

Categories