Removing last bit of a string including separator? [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 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]+$/,''));

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

JavaScript Regular Expression Find and Replace [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 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);

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!!");
}

Remove () and - and white spaces from phone number in Javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a phone number like (123) 456-7891. I need number like 1234567891.
How can I do that in Javascript ?
To be on the safe side, I would recommend removing everything except + (for country codes) and digits:
result = subject.replace(/[^+\d]+/g, "");
You can use String.replace() to do this. Pass it a RegExp that matches everything but digits and replace them with ''.
var number = '(123) 456-7891';
number = number.replace(/[^\d]/g, '');
alert("(123) 456-7891".replace(/[\(\)\-\s]+/g, ''));
please check this link.it might help you
http://www.w3resource.com/javascript/form/phone-no-validation.php
there are many examples here.it might be useful for you.

Get the Particular part of the textbox ID in jquery [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the Text box .That text Box ID is text_1__val.
I need 1 from that Id.How to Get the Particular part of the textbox ID in jquery which means i need the between _ and __ from that textbox ID?
If the only requirement is that the character / characters appear between a single and double underscore, try this regular expression match
var rx = /_(.+?)__/;
var part = rx.test(idValue) && rx.exec(idValue)[1];
This assumes that you're only after the first of any occurrences in your ID value string. If the string fails to match, part will be false.
The split() method is used to split a string into an array of substrings, and returns the new array.
$(your_textbox).attr("id").split("_")[1]
//Syntax
string.split(separator,limit)
Function Reference: http://www.w3schools.com/jsref/jsref_split.asp

Categories

Resources