How to remove the character form string using jQuery? [duplicate] - javascript

This question already has answers here:
Regex to remove letters, symbols except numbers
(6 answers)
Strip all non-numeric characters from string in JavaScript
(12 answers)
Closed 6 years ago.
How to remove character from strings (a-z, A to Z) using jQuery or JavaScript?
if str=avc234jw6;
I need only 2346.

A simple String.replace with regex /[a-z]/ig can do!
var str = "avc234jw6";
var no = parseInt(str.replace(/[a-z]/ig, ""));
console.log(no);

Answer on non-edited question "How to remove char from strin?"
var removeChar = "a";
var string = "abc abc abc";
function removeFromString(char, string){
var newString = "";
for (var i = 0; i < string.length; i++) {
if(string[i].toLowerCase() == char){
continue;
} else {
newString += string[i];
}
}
return newString;
}
$("#test").html(removeFromString(removeChar, string));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
test
</div>

Related

NodeJS How to pad the end of numbers with . and zeros? [duplicate]

This question already has answers here:
Rounding numbers to 2 digits after comma
(10 answers)
Closed 3 years ago.
Let's say we have the number 300 and I wanted it to be padded to end as 300.000
Or the number 23,5 would be something like 23.500
Hope it will help, details are in comments.
function formating(n) {
// Convert number into a String
const str = n.toString();
// Find the position of the dot.
const dotPosition = str.search(/\./);
// Return string with the pad.
if (dotPosition === -1) {
return str += ".000";
} else {
const [part1, part2] = str.split('.');
return part1 + '.' + part2.padEnd(3, "0");
}
}

Is there a way to make just the first letter upper case which is the purpose of the code [duplicate]

This question already has answers here:
Auto Capitalize ONLY the First Letter of Each Word in an Input Field
(2 answers)
Closed 3 years ago.
The purpose of this code is to make the first letter of every word passed to the function uppercase, but it end up returning uppercase for all position were the first letter is repeated.
function c(x) {
var y, z = x[0];
y = x.split(z).join(z.toUpperCase());
return y;
}
console.log(c("emmanuel"));
function c1(x) {
return x.charAt(0).toUpperCase() + x.slice(1);
}
function c2(x) {
return x[0].toUpperCase() + x.slice(1);
}
console.log(c1("test string"));
console.log(c2("test string"));
function c(x) {
var ARR;
ARR = x.split("");
ARR[0]=ARR[0].toUpperCase();//<<< then you should also
//uppercase just
// the first char
return ARR.join("");
}
console.log(c("test string"));
there is also a css rule which do the trick...
css capitalize
same just for a whole sentence
<script>
function titlelize(s) {
var ARR,WORDS;
WORDS = s.split(" "); // separate sentencein words
WORDS[i]=WORDS[i].toLowerCase();//ensure all other will be lowercase
for (var i = 0; i < WORDS.length; i++) { //run over each word
ARR = WORDS[i].split(""); // split word in chars
ARR[0] = ARR[0].toUpperCase() //make first car uppercase
WORDS[i] = ARR.join(" "); //recreate word from char
}
return WORDS.join(" ") // recreate sentence
}
</script>

Javascript replace string by another String [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
I have following string in javascript.
var str = 'P24 + P33'; //p24 is just exp. it will be any number i.e. P98
I Want to replace this string into following string using jquery replace.
var str = "$('#p24').val() + $('#p33').val()";
var str = 'P24 + P33'; //p24 is just exp. it will be any number i.e. P98
var str_array = str.split(" + ");
console.log("Original string: "+str);
for(var i = 0; i < str_array.length; i++){
str_array[i] = $("#"+str_array[i]).html();
}
var replaced_string = str_array.join(" + ");
console.log("Replaced string: "+replaced_string);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="P24">this is P24</div>
<div id="P33">this is P33</div>

How to capitalize the first letter of word in a string using JavaScript [duplicate]

This question already has answers here:
Capitalize words in string [duplicate]
(21 answers)
Closed 6 years ago.
I am trying to use javascript to capitalize the words of a sentence. For example
var input = "I am not bad with javascript."
var output = "I Am Not Bad With Javascript."
I have the following codes and I can't figure out why my replace did not work.. Thank You
var loop = function(collection, callback){
// ultimate side-effects function
for (var i = 0; i < collection.length; i++){
callback(collection[i]);
}
};
var capitalizeFirstLetters = function(string) {
// have a new string
// split the string into substring
// use the loop function to find the "space"and CAP letter after
var newString = [];
var subString = string.split(' ');
loop(subString, function(word){
subString.replace(word[0], word[0].toUpperCase());
return newString.push(subString);
})
return newString.join(' ');
}
Here this should work
function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}
document.write(titleCase("I'm a little tea pot"));
You could try something like this:
var input = "I am not bad with javascript."
input = input.split(" ")
.map(function(item){
return item[0].toUpper()+item.slice(1);
})
.join(" ");
var input = "I am not bad with javascript."
input = input.split(" ")
.map(function(item){
return item[0].toUpperCase()+item.slice(1);
}).join(" ");
alert(input);

Java Script - Extract number from string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How might I extract the number from a number + unit of measure string using JavaScript?
How to extract number from string like this in JS.
String: "Some_text_123_text" -> 123
JSFiddle Demo
var s = "Some_text_123_text";
var index = s.match(/\d+/);
document.writeln(index);​
try this
var string = "Some_text_123_text";
var find = string.split("_");
for(var i = 0; i < find.length ; i ++){
if(!isNaN(Number(find[i]))){
var num = find[i];
}
}
alert(num);
try this working fiddle
var str = "Some_text_123_text";
var patt1 = /[0-9]/g;
var arr= str.match(patt1);
var myval = arr.join("");

Categories

Resources