How to remove character after # in javascripts? - javascript

How to remove character after # in javascripts ?
for example, I have string 'babibu#blabla.com', I want to get string before # 'babibu', how to do that?

There are various ways to do this:
1. Regex
Use the regex /^(.*)#/
Regex explanation
Demo:
var email = 'babibu#blabla.com';
var username = email.match(/^(.*)#/)[1];
console.log(username);
2. Array.split()
var email = 'babibu#blabla.com';
var username = email.split('#', 1)[0];
console.log(username);

Alternative to regex:
var username = email.split('#')[0];

Related

RegEx to identify guid or a number

I have regex for guid:
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
And regex to match a number:
/^[0-9]*$
But I need to match either a guid or a number. Is there a way to do it matching a single regex. Or should I check for guid and number separately.
Use | in a regular expression to match either what comes before or what comes after:
const re = /^([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})|[0-9]+$/i;
console.log('11111111-1111-1111-8111-111111111111'.match(re)[0]);
console.log('5555'.match(re)[0]);
You can use | operator between two regex expressions. You can use them as given below
/(^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i) | (^[0-9]*$)/
Yes, there are many ways for this. One is to OR the matches in an if statement:
var reg1 = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
var reg2 = /^[0-9]*$/;
var text = " ... ";
if (reg1.test(text) || reg2.test(text)) {
// do your stuff here
}
Another approach is ORing in the RegExp itself:
var regex = /(^[0-9]*)|^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
var text = " ... ";
if (regex.test(text)) {
// do your stuff here
}

Regular Expression: password must contain at least 1 special character

I am trying to verify that the password inserted by the user has atleast 1 special character in it. Can anyone help me with the regExp?
var password = document.getElementById(.....).value;
var special = new RegExp("?????");
if(special.test(password)){
......
}
I consider all the non-word characters as special characters.
var special = new RegExp("^.*?\\W");
OR
var special = /^.*?\W/;
use this way.
var passwd = document.getElementById('password').value;
var pattern = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/);
if (pattern.test(passwd)) {
// code goes here
}

Regular expression extract string

I have a string "#{Name; 11112121#xyz.com}"
I want to write a regular expression that extracts Name and 11112121 from the above string
This is what I tried.
function formatName(text){
var regex = /#\{([^;]+); ([^\}]+)\}/
return text.replace(
regex,
'$1, $2'
);
}
The above gives Name, 11112121#xyz.com. But I want only Name, 11112121
try this
var regex = /#\{([^;]+); ([^\}]+)(#.*)\}/
$1 => Name
$2=> 11112121
Here is the working example
Use match instead, like this:
var regex = /#\{([^;]+);\s+([^#]+)/;
var matches = text.match(regex);
alert(matches[1] + ', ' + matches[2]);
http://jsfiddle.net/rooseve/bM2U6/
If you want to match everything until the # character, you can use
var regex = /#\{([^;]+); ([^#]+)/
If you need to verify that the string also contains a } after that, you can add that to the end:
var regex = /#\{([^;]+); ([^#]+)[^}]*\}/

Regex to get string in URL before optional character

I have a URL which may be formatted like this: http://domain.com/space/all/all/FarmAnimals
or like this: http://domain.com/space/all/all/FarmAnimals?param=2
What regular expression can I use to return the expression FarmAnimals in both instances?
I am trying this:
var myRegexp = /\.com\/space\/[a-zA-Z0-9]*\/[a-zA-Z0-9]*\/(.*)/;
var match = myRegexp.exec(topURL);
var full = match[1];
but this only works in the first instance, can someone please provide an example of how to set up this regex with an optional question mark closure?
Thank you very much!
/[^/?]+(?=\?|$)/
Any non-/ followed by either ? or and end-of-line.
I wouldn't write my own regex here and let the Path class handle it (if those are your two string formats).
string url = "http://domain.com/space/all/all/FarmAnimals";
//ensure the last character is not a '/' otherwise `GetFileName` will be empty
if (url.Last() == '/') url = url.Remove(url.Length - 1);
//get the filename (anything from FarmAnimals onwards)
string parsed = Path.GetFileName(url);
//if there's a '?' then only get the string up to the '?' character
if (parsed.IndexOf('?') != -1)
parsed = parsed.Split('?')[0];
You could use something like this:
var splitBySlash = topURL.split('/')
var splitByQ = splitBySlash[splitBySlash.length - 1].split('?')
alert(splitByQ[0])
Explanation:
splitBySlash will be ['http:','','domain.com', ... ,'all','FarmAnimals?param=2'].
Then splitByQ will grab the last item in that array and split it by ?, which becomes ['FarmAnimas','param=2'].
Then just grab the first element in that.
This
.*\/(.*?)(\?.*)?$
Should capture the part of the string you are looking for as the group 1 (and the query after ? in group 2, if needed).
var url = 'http://domain.com/space/all/all/FarmAnimals?param=2';
//var url = 'http://domain.com/space/all/all/FarmAnimals';
var index_a = url.lastIndexOf('/');
var index_b = url.lastIndexOf('?');
console.log(url.substring(index_a + 1, (index_b != -1 ? index_b : url.length)));

How Can I Use This Regex With JavaScript

I have a regex and using it in PHP:
(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z])(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))
I want it to use with javascript like:
regexp = new Regexep("here that regular expression");
and check with:
regexp.text(data)
But I couldn't do it work.
Please help me
Thanks,
var matches = data.match(/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z])(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/);
or
var regex = /(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z])(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/;
var matches = regex.test(data);

Categories