Nodejs regexp validation for sip URI string - javascript

var uriRegExp = /^(sip):\(?([0-9]{3})\)?[- ]?([0-9]{3})[- ]?([0-9]{4})#\w+(\ w+)*(\.\w)(\.\w{2,3})+$/;
Is this a correct regular expression for validating the string
sip:1-999-123-4567#voip-provider.example.net ?

No, this regex does not match your string.
If you want to know why you could have a look at https://regex101.com/r/EC0xFN/1 .
There you can interactively build and check your regex with different input strings.

Here is a simple sip URI validator, that i have created, using regular expression.
function myFunction() {
var str = "sip:+91989556926#test.est.test.com";
var regExp = /^(sip):(\S+[0-9])#\S+(\w+([.-]?\w+)*).(\w{2,3})$/;
var result = regExp.test(str);
document.getElementById("demo").innerHTML = result;
}
Please check the link.
[https://regex101.com/r/5vMfI9/4][1]

Related

How do I pass a variable into regex with Node js?

So basically, I have a regular expression which is
var regex1 = /10661\" class=\"fauxBlockLink-linkRow u-concealed\">([\s\S]*?)<\/a>/;
var result=text.match(regex1);
user_activity = result[1].replace(/\s/g, "")
console.log(user_activity);
What I'm trying to do is this
var number = 1234;
var regex1 = /${number}\" class=\"fauxBlockLink-linkRow u-concealed\">([\s\S]*?)<\/a>/;
but it is not working, and when I tried with RegExp, I kept getting errors.
You can use RegExp to create regexp from a string and use variables in that string.
var number = 1234;
var regex1 = new RegExp(`${number}aa`);
console.log("1234aa".match(regex1));
You can build the regex string with templates and/or string addition and then pass it to the RegExp constructor. One key in doing that is to get the escaping correct as you need an extra level of escaping for backslashes because the interpretation of the string takes one level of backslash, but you need one to survive as it gets to the RegExp contructor. Here's a working example:
function match(number, str) {
let r = new RegExp(`${number}" class="fauxBlockLink-linkRow u-concealed">([\\s\\S]*?)<\\/a>`);
return str.match(r);
}
const exampleHTML = 'Some link text';
console.log(match(1234, exampleHTML));
Note, using regex to match HTML like this becomes very order-sensitive (whereas the HTML itself isn't order-sensitive). And, your regex requires exactly one space between classes which HTML doesn't. If the class names were in a slightly different order or spacing different in the <a> tag, then it would not match. Depending upon what you're really trying to do, there may be better ways to parse and use the HTML that isn't order-sensitive.
I solved it with the method of Adem,
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
var number = 1234;
var firstPart = `<a href="/forum/search/member?user_id=${number}" class="fauxBlockLink-linkRow u-concealed">`
var regexpString = escapeRegExp(firstPart) + '([\\s\\S]*?)' + escapeRegExp('</a>');
console.log(regexpString)
var sample = ` `
var regex1 = new RegExp(regexpString);
console.log(sample.match(regex1));
in the first place the issue was actually the way I was reading the file, the data I was applying the match on, was undefined.

Regular expression for domain validation in nodejs

Hi I want to validate if only a domain is present in a string. For example the string "https://anyDomain" or "https://anyDomain/" should return true. Any other strings like "https://anyDomain/in" should return false.
The regex code that I have currently returns true for "https://anyDomain" but not for "https://anyDomain/". Below is the code:
var url = "https://anyDomain/";
var reg = /^https:\/\/[^\/]*$/;
console.log(reg.test(url));
Try the following regex:
^https:\/\/.*.com(\/)?

Regexp match everything after a word

I am trying to extract the base64 string from a data-url. The string looks like this, so I am trying to extract everything after the word base64
test = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB'
So I want to extract the following from the above string
,/9j/4AAQSkZJRgABAQAAAQAB
Here is my regex
const base64rgx = new RegExp('(?<=base64)(?s)(.*$)');
console.log(test.match(base64rgx))
But this fails with the error:
VM3616:1 Uncaught SyntaxError: Invalid regular expression: /(?<=base64)(?s)(.*$)/: Invalid group
var test = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB';
var regex = /(?<=base64).+/;
var r = test.match(regex);
console.log(r);
Here's the regex: https://regex101.com/r/uzyu0a/1
It appears that lookbehinds are not being supported. But the good news is that you don't need a lookaround here, the following pattern should work:
test = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB'
var regex = /.*?base64(.*?)/g;
var match = regex.exec(test);
console.log(match[1]);
I'm not sure precisely how much of the string after base64 you want to capture. If, for example, you don't want the comma, or the 9j portion, then we can easily modify the pattern to handle that.
You may not need regular expression: just find base64 string with indexOf and use substr
var test = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB';
var data = test.substr(test.indexOf('base64')+6); // 6 is length of "base64" string
console.log(data);

Javascript Regex with Match

I am trying to extract a number from a string with regular expression as I am told this would be the best approach for what I am wanting to do.
Here is the string:
http://domain.com/uploads/2011/09/1142_GF-757-S-white.jpg&h=208&w=347&zc=1&q=90&a=c&s=&f=&cc=&ct=
and I am trying to extract 208 from (height) from the string. so I know I have to look for "&h=" in the expression but I don't know what to do after that. How can I match between that and the next "&" but not include them as well...
Thanks..
Regular expression to match an h url parameter containing an integer value.
[&?]h=(\d+)
The Javascript:
var match = /[&?]h=(\d+)/.exec(url_string);
alert(match[1]);
Learn more about Regular Expressions.
To get the entire h=xxxx parameter, you can use this generic function (which you can reuse elsewhere for other purposes) and pass it the desired key:
function getParameterFromURL(url, key) {
var re = new RegExp("[&?]" + key + "=([^&]*)");
var matches = url.match(re);
if (matches) {
return(matches[1]);
}
return(null);
}
var url = "http://domain.com/uploads/2011/09/1142_GF-757-S-white.jpg&h=208&w=347&zc=1&q=90&a=c&s=&f=&cc=&ct=";
var parm = getParameterFromURL(url, "h");
See http://jsfiddle.net/jfriend00/86MEy/ for a working demo.

Parse string in javascript

How can I parse this string on a javascript,
var string = "http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1";
I just want to get the "265956512115091" on the string. I somehow parse this string but, still not enough to get what I wanted.
my code:
var newstring = string.match(/set=[^ ]+/)[0];
returns:
a.265956512115091.68575.100001022542275&type=1
try this :
var g=string.match(/set=[a-z]\.([^.]+)/);
g[1] will have the value
http://jsbin.com/anuhog/edit#source
You could use split() to modify your code like this:
var newstring = string.match(/set=[^ ]+/)[0].split(".")[1];
For a more generic approach to parsing query strings see:
Parse query string in JavaScript
Using the example illustrated there, you would do the following:
var newstring = getQueryVariable("set").split(".")[1];
You can use capturing group in the regex.
const str = 'http://www.facebook.com/photo.php?fbid=322916384419110&set=a.265956512115091.68575.100001022542275&type=1';
console.log(/&set=(.*)&/.exec(str)[1]);

Categories

Resources