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"
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
The documentation specifies that the first arg in parseInt is the number you want mutated and the second arg is the base you want to convert it to. But when I try to use it as such it returns NaN instead of an integer. Why is this?
parseInt(65, 60)
>>>NaN
From http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2
Step 8a:
If R < 2 or R > 36, then return NaN.
That's why.
It's just a arbitrary rule to simplify implementation, probably.
Edit: see comment for the probable reason.
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.
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
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.
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