JavaScript Regular Expression Find and Replace [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need to check if a string contains the following string, "Password:". If so I want to replace the word immediately following the ':'. For example, I have a string that has "Password:Test". I would like "Test" removed and replaced with "Removed".

You can use the following (please see edit if this doesn't work in your browser):
var input = 'Password:Test'
console.log(input.replace(/(?<=Password:).+/, 'Removed'));
Edit
As #ctwheels pointed, lookbehinds have little support in JavaScript (see the current stage of the TC39 proposal here). At the time of writing this only Chrome (starting with version 62) and Moddable (after Jan 17, 2018) support lookbehinds in JavaScript. Use the following instead:
Regex: (Password:).+ Substitution: $1Removed
var input = 'Password:Test'
console.log(input.replace(/(Password:).+/, '$1Removed'));

This works Run code:
var string = "Passoword:Test";
var find = string.indexOf(":");
var newstring = string.substr(0,find) + ":" + "Removed";
console.log(newstring);

Related

Check whether string includes some substring of another string - the easiest way [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Clarification - I want to check if str1 and str2 share common substring.
str1 = "and we know that the lion"
str2 = "the lion is big"
by using those two string because the lion happens to show on both string then true will be invoked.
Thanks.
You can use String.includes that will return a boolean value:
console.log("and we know that the lion".includes("the lion is big")); // returns false
console.log("and we know that the lion".includes("the lion")); // returns true

In javascript,Can I store emojis in an array as array's element? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to store emojis in an array and print it using console.out(); on the browser's console ?
They're just characters so you can use Unicode escapes. Just find the one you want and use \u plus the codepoint value: \u{xxxxx} where xxxxx is the codepoint. The syntax with the curly braces only works in ES6. You can only use four hex digits with the original \uxxxx format so if you're stuck pre-ES6 you have to use "surrogate pairs," which is explained in the linked article.
var a = [ '\u{1F984}' ];
console.log(a[0]);
Running this in Node:
$ node
> var a = [ '\u{1F984}' ];
undefined
> console.log(a[0]);
🦄
undefined
You should see a Unicorn--a white horse with a horn.
If you don't see a Unicorn then maybe you're using a terminal that doesn't support Unicode. I'm using the macOS terminal. Try it in your browser JavaScript console.

Removing last bit of a string including separator? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Let's say I have a string that looks like this:
Red#Yellow#Blue#Green
How can I use Javascript to remove the last instance of # as well as the text that comes after it, so that the resulting string would look like this:
Red#Yellow#Blue
string=string.split("#");
alert(string.pop());//Green
string=string.join("#");
I dont see a problem? Simply split by #, remove the last one and join again?
you can split the string into arrays and join all the items of the array except the last one
var myString = Red#Yellow#Blue#Green;
var myArrray = myString.split('#');
myArray.splice(myArray.length-1,1);
myArray.join('#');
console.log('Red#Yellow#Blue#Green'.replace(/\#[a-zA-Z]+$/,''));

What will be the regular expression for following date and time format like YYYYMMDD.HHMMSS.? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to validate the textbox as per date & time format like YYYYMMDD.HHMMSS so I required regular expression for date & Time using the javascript.
Please let me know the answer as soon as possible.
Required RE for YYYYMMDD.HHMMSS date Time format.
Dear, Here is your answer :
var r = /^((((19|[2-9]\d)\d{2})[\/\.-](0[13578]|1[02])[\/\.-](0[1-9]|[12]\d|3[01])\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((19|[2-9]\d)\d{2})[\/\.-](0[13456789]|1[012])[\/\.-](0[1-9]|[12]\d|30)\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((19|[2-9]\d)\d{2})[\/\.-](02)[\/\.-](0[1-9]|1\d|2[0-8])\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))[\/\.-](02)[\/\.-](29)\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])))$/g;
r.test('2014-06-24 15:49:05'); // true
r.test('2016-02-29 23:19:20'); // true [leap year]
r.test('2015/02/29 23:19:20'); // false
r.test('2010.11.31 10:00:02'); // false
r.test('1956.10.12 24:10:02'); // false
Thanks & Cheers

Regex phonenumber [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can someone help me with a regex expression for a phone number
it needs to be in this format only
xxx-xxx-xxxx
Try this
^\d{3}\-\d{3}\-\d{4}$
Multiple ways are there. For example:
var regex = /^\d{3}-\d{3}-\d{4}$/;
console.log(regex.test('999-999-9999'));
console.log(regex.test('9999-999-99999'));
//OR
var regex2 = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/
console.log(regex2.test('999-999-9999'));
console.log(regex2.test('9999-999-99999'));
You can also write specific to a country or area. See this example.
If you are taking it from user input validate it like this:
var val = document.getElementbyId('yourInputId').value;
if(regex2.test(val)){
alert("Success!!");
}
else{
alert("Failure!!");
}

Categories

Resources