How to Split string before '#' in angularJS [duplicate] - javascript

This question already has answers here:
How can I extract the user name from an email address using javascript?
(9 answers)
Closed 4 years ago.
I want to split an email of user before letter
#
in Javascript especialy in angularJS. for example if its
blabla#gla.com
it will turn into
blabla
can someone give me a simple example to make it?
because i must split it from API and store it as localstorage,
some of example that i find its use limitTo but can we use it to cut it in specific way from # until end?

Try this str.split()
var email = "blabla#gla.com";
var userName = email.split('#');
console.log(userName[0]);
Or use str.substring()
var userName = email.substring(0, email.indexOf('#'));
console.log(userName);

Related

Replace multiple strings in a text file with one string using javascript [duplicate]

This question already has answers here:
Replace a line in txt file using JavaScript
(2 answers)
Closed 1 year ago.
I'm trying to replace multiple strings in a text file with one string using javascript
I want those two strings "user" and "password" must be replaced with 'replace the string'
Anyone help me here?
to add to the previous replies, you can use capture groups to back reference parts of the matched text i.e.
if you search for "user"
let newText = yourText.replace(/^((\"user\")(:".*")(.*))$/gm, '"removed"$3$4' );
if you search for "1232"
let newText = yourText.replace(/^((\".*\")(:"1232")(.*))$/gm, '"removed"$3$4' );

Regex how to get what comes after a certain word in a string? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 2 years ago.
http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903
I am trying to get whatever comes after 'code=' from this string. How could I do this?
You could use
code=(.+)
See a demo on regex101.com.
In JavaScript this could be:
let string = 'http://localhost:3000/?code=85b1a3645tgreg1f54221d8d9f54923b88ade29945yttrtgdg903';
let m = string.match(/code=(.+)/);
console.log(m[1]);
But it would possibly be more straight-forward to parse the url as it is and use the query part accordingly.
str.match(/code=(.*)/)[1] will do that for you.

JS - Transform single email validation pattern into multiple email validation pattern. [duplicate]

This question already has answers here:
Javascript multiple email regexp validation
(7 answers)
Validate a comma separated email list
(5 answers)
How to write regex to verify a comma delimited list of values
(4 answers)
Closed 4 years ago.
I am struggling to create a Regex pattern that would validate multiple emails. I know that this topic has been widely discussed, however, having researched them all I could not find the answer to my specific question. My issue is as follows.
The project I am working on is written in PHP and uses FILTER_VALIDATE_EMAIL. I aimed at writing a front-end email validator to use for both single and multiple emails in a way, consistent with FILTER_VALIDATE_EMAIL. Here https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js I have found the ideal Regex pattern compliant with latest RFC standard provisions, namely:
^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}#)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*#(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126})+(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))]))$
Now I've been trying to make this particular pattern function for multiple emails. Here comes my question, how can I get it done? I was trying to set
[\s*,]*
in order to make multiple addresses pass, by so far all I got is a headache, so your assistance is highly welcome, thanks in advance!
Try this..
var pattern = /^\b[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i
if(!pattern.test(inputYour))
{
console.log('not a valid e-mail address');
}​
for multiple email..
var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
validate(email.trim());
});
validate function should have your above code...
This is for check in one go...
var email = 'test#example.com, hello#example.com,mail#example.com';
alert ( (/^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#(([^<>()[\]\.,;:\s#\"]+\.)+[^<>()[\]\.,;:\s#\"]{2,})$/i.test(email)) );
link https://www.experts-exchange.com/questions/28940315/JavaScript-REGEX-validate-multiple-emails-addresses-separated-my-commas.html

Restrict duplicate words in email regular expression javascript/jquery [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 6 years ago.
I want to restrict duplicate words in an email using regex.
Currently I am using below regex that also restrict 2 consecutive dots (ex: a#gmail..com)
regexp: /A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*##(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z][{a-z},0-9](?:[a-z0-9-]*[a-z0-9]){1-2}?)$/i,
Now I want to also restrict duplicate words in email for example :-
abc.xyz#gmail.com.com
It should not accepted second .com .
What about:
email="hello.world.world";
email=email.split(".");
fail = email.some((ae,ai)=>email.some((be,bi)=>bi==ai?false:ae==be));
//loop trough all pairs, if they are at the same index skip ( we dont want to check email[0]==email[0]), else compare, if same fail.
if(fail){alert("Wrong email");}

handling special characters in parameters with XMLHttpRequest [duplicate]

This question already has answers here:
Pass a parameter containing '%' in URL?
(2 answers)
Closed 6 years ago.
Trying to send password using xmlHttpRequest from frontend(javascript) with POST and other parameters with names like
"&password=" document.getElementById('password').value
I'm using HttpServletRequest.getparameter to get the parameter string of password like string pswd = request.getparameter("password");
The code works fine with all passwords that have special characters except passwords like these qwe100%qwe, qwe198%qwe
When I pass those passwords, if I were to read username or other parameters sent along with password also results in exception[illegalArgumentException]
any help is appreciated..
You need to use encodeURIComponent on the JavaScript side before passing it to the back end.
That is:
var pass = encodeURIComponent(document.getElementById('password').value);
Note that on the Java side, you should then do a:
URLDecoder.decode(request.getParameter("password"));
EDIT
As #BalusC points out I'm wrong on the Java side - you have already done what is needed on the JavaScript side so you don't need the Java part.

Categories

Resources