How to detect only digit ,'(' , ')' ,'+' in Javascript String [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I would like to detect if a JavaScript String contains one of these following characters :
a digit ( 0 to 9)
the '(' character
the ')' character
How can i do that ?

var s = 'your string';
if (/[0-9()+]/.test(s)) {
// match
}
0-9 can be written as \d
if (/[\d()+]/.test(s)) {}
Your question is inconsistent. If you want to test if string is '0' or '1' ... or '(':
if (/^[\d()+]$/.test(s)) {}
Read about RegEp here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Related

Javascript remove leading zeros and decimal points from string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
searching for a regex to convert
0.015.000 -> 15.000
0.150.000 -> 150.00
015.000 -> 15.00
You could try the below string.replace function. Use ^ to tell the regex engine to do the matching operation from the start. By putting 0 and . inside a character class would match either 0 or dot.
string.replace(/^[0.]+/, "")
Example:
> "0.015.000".replace(/^[0.]+/, "")
'15.000'
> "0.150.000".replace(/^[0.]+/, "")
'150.000'
> "015.000".replace(/^[0.]+/, "")
'15.000'

How to get total number of vowels in a string [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 am just curious how you can get the total number of vowels in a string. I know this is a very simple function, but I am new to Javascript. Thanks for the help
You can remove all characters that are not vowels and count the remaining ones:
var str = /* something*/,
numberVowels = str.replace(/[^aeiou]/ig, '').length;
Where
/[^aeiou]/ matches single character NOT present in the list "aeiou"
i is the case-insensitive flag, to count AEIOU too.
g is the global flag, to replace ALL characters which are not vowels, instead of just the first one.

How to check digit from string [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 getting string. I want to check string have any digit.
The string may be -
var val="1"; or val=",,"; or val = "1,2,0";
I want to check if val have any digit.
function hasDigit(yourString) {
return yourString.match(/\d/) !== null;
}
Here's a fiddle demo: http://jsfiddle.net/VpLgG/
You can use yourInputString.match(/[0-9]/g), or what Jonah has specified. This will return null if there are not digits else it will return the values present inside the string that will match separated by comma.
Javascript side :
if (/\d/.test(val)) {
// do something
}
Not really useful but required to get attention apparently (demo here)...
function hasDigit(val) {
return /\d/.test(val);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

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.

How to replace number in this string using 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
Can someone tell me how to replace the number "2" with another number in this string"? For example, if it's a 2, it should be a 3, if it's a 3 it should be a 4, etc.
Please note that the number can be any number from 1 through 5.
/img/tmp/2_th.jpg
Just use String#replace:
s.replace(/\d+(?=_)/, "replaced");
To replace any number from 1-5 use:
s.replace(/[1-5](?=_)/, "replaced");
Here (?=_) is used a positive lookahead which makes sure match a number which is followed by underscore _.
UPDATE: Based on your edit you can use this code to increment matched number by 1:
s.replace(/([1-5])(?=_)/, function(n) {return parseInt(n)+1;});
Read More About Lookarounds in Regex
I understand from your question that you want to replace any number by the following one (" if it's a 2, it should be a 3, if it's a 3 it should be a 4").
Then you can do this :
var input = "/img/tmp/2_th.jpg";
var output = input.replace(/\d+/g,function(s){ return +s+1 })
Result :
"/img/tmp/3_th.jpg"

Categories

Resources