want to read number of words and charecter using javascript - javascript

consider, in my javascript i am getting :
function callBack_Show(result) {
//where result is in jsonp format
var artical= result.Artical;
now artical contains some text,
i want to read number of words and characters in it and display them
the words can be separated by: blank space, fullstop,comma etc
and i want to do it in same javascript function (callBack_Show(result) )

To get the number of words, you can split the text using a regular expression where you can define what is your criteria for separating words.
To get the number of characters, you can use the length property of the String, eg.:
var words = artical.split(/[\s.,]/).length; // whitespace, dot and comma
var characters = artical.length;

To calculate the number of words you want to use the String.split() method, and then use the length property of the resultant Array. To count the number of characters use the String.length property.

Related

Extract numeric and text parts of a string, in varying formats

I'm trying to put together a RegEx to split a variety of possible user inputs, and while I've managed to succeed with some cases, I've not managed to cover every case that I'd like to.
Possible inputs, and expected outputs
"1 day" > [1,"day"]
"1day" > [1,"day"]
"10,000 days" > [10000,"days"]
Is it possible to split the numeric and text parts from the string without necessarily having a space, and to also remove the commas etc from the string at the same time?
This is what I've got at the moment
[a-zA-Z]+|[0-9]+
Which seems to split the numeric and text portions nicely, but is tripped up by commas. (Actually, as I write this, I'm thinking I could use the last part of the results array as the text part, and concatenate all the other parts as the numeric part?)
var test = [
'1 day',
'1day',
'10,000 days',
];
console.log(test.map(function (a) {
a = a.replace(/(\d),(\d)/g, '$1$2'); // remove the commas
return a.match(/^(\d+)\s*(.+)$/); // split in two parts
}));
This regular expression works, apart from removing the comma from the matched number string:
([0-9,]+]) *(.*)
You cannot "ignore" a character in a returned regular expression match string, so you will just have to remove the comma from the returned regex match afterwards.

Regex to accept only variable names

How do I write a regex that accepts only words or letters and split them by ,?
I have tried array = input.replace(/ /g, '').split(','), but then h-e,a<y will become ['h-e','a<y'] I want to accept only variables, so I guess h-e,a<y should become ['he','ay'].
Would it be something like
array = input.replace(/[\s|^\w]/g, '').split(',')
You could use this regex to find all the characters that you want to remove from your string:
array = input.replace(/[-><?.:;]*/ig, '').split(',')
You will replace all the characters that are inside the [ ].
Split first, then remove characters not allowed:
input.split(,).map(fix)
where
function fix(s) {
return s.replace(/[^\w$]/g, '');
}
Another approach is to grab the characters you want, instead of throwing away the ones you don't:
function fix(s) {
return s.match(/[\w$]/g).join('');
}
Actually, this is not exactly right, because JavaScript variable names can also contain Unicode characters such as Σ. Also, this would not fix up leading numeric characters, which JavaScript variable names cannot start with.

javascript getting a faulty result using a regular expression

In my web page, I have:
var res = number.match(/[0-9\+\-\(\)\s]+/g);
alert(res);
As you can see, I want to get only numbers, the characters +, -, (, ) and the space(\s)
When I tried number = '98+66-97fffg9', the expected result is: 98+66-979
but I get 98+66-97,9
the comma is an odd character here! How can eliminate it?
Its probably because you get two groups that satisfied your expression.
In other words: match mechanism stops aggregating group when it finds first unwanted character -f. Then it skips matching until next proper group that, in this case, contains only one number - 9. This two groups are separated by comma.
Try this:
var number = '98+66-97fffg9';
var res = number.match(/[0-9\+\-\(\)\s]+/g);
// res is an array! You have to join elements!
var joined = res.join('');
alert(joined);
You're getting this because your regex matched two results in the number string, not one. Try printing res, you'll see that you've matched both 98+66-979 as well as 9
String.match returns an array of matched items. In your case you have received two items ['98+66-97','9'], but alert function outputs them as one string '98+66-97,9'. Instead of match function use String.replace function to remove(filter) all unallowable characters from input number:
var number = '98+66-97fffg9',
res = number.replace(/[^0-9\+\-\(\)\s]+/g, "");
console.log(res); // 98+66-979
stringvariable.match(/[0-9\+\-\(\)\s]+/g); will give you output of matching strings from stringvariable excluding unmatching characters.
In your case your string is 98+66-97fffg9 so as per the regular expression it will eliminate "fffg" and will give you array of ["98+66-97","9"].
Its default behavior of match function.
You can simply do res.join('') to get the required output.
Hope it helps you
As per documents from docs, the return value is
An Array containing the entire match result and any parentheses-captured matched results, or null if there were no matches.
S,your return value contains
["98+66-97", "9"]
So if you want to skip parentheses-captured matched results
just remove g flag from regular expression.
So,your expression should like this one
number.match(/[0-9\+\-\(\)\s]+/); which gives result ["98+66-97"]

JavaScript strings manipulations

I have a string like this below:
var stOrig= "ROAM-Synergy-111-222-LLX" ;
There can be any no. of "alphabetic" terms before numeric values 111-222..
There may or may not be any numeric values i.e the string can also be simply like this:
"ROAM-Synergy-LCD-ROAM".
if there are numeric values then I am using this
var myval = st.match(/^\D+(?=-)/)[0];
to get only the alphabetic terms before the numeric values. Its working fine till here.
But if suppose string does not contains any numeric values then my regular expression returns one less term i.e
Say the original string is: "ROAM-Synergy-LCD-ROAM" (without any numbers in it.)
Now if is use above reg expression...then it will return only "ROAM-Synergy-LCD"
..
so first I need to check for any numeric values in original string.. and if string contains numeric values then I use above reg exp...but please suggest If string does not contain numeric values then what reg expression to use..
Use
var myval = st.match(/^\D+(?=-|$)/)[0];
The $ matches at the end of the string.
See it live on regex101.com.

How can I use substring to print delimited string

How can I print the first few characters of a delimited string by using substring()
var txt="AB:CD:EF:GH:IJ:KL:MN:OP";
document.write(txt.substring(3));
If you want to get first 3 characters - you should use substring(0,3)
For first three "chunks":
txt.split(":").slice(0,3).join(":")
will give you "AB:CD:EF"
If you want to find the characters before the first colon, use
var txt="AB:CD:EF:GH:IJ:KL:MN:OP";
document.write(txt.split(":")[0]);
txt.substring(start, end)
If you want the first few, starting at the beginning it would be txt.substring(0,3); or substitute a number for 3 wherever you want it to stop.

Categories