Stripping some characters out of a string - javascript

I try to remove some non-safe characters from a string but i believe i have a problem on my RegExp object.
What i try to do below is if there are chars whose encoded length is greater than 3 chars they should be replaced with a space.
So if encoded value is %3D which is = sign, it is ok to have in my string. But if it is an ’ apostrophe %E2%80%99 it should be replaced with a space.
val = "Angelina’s";
valEnc = encodeURIComponent(val);
for(var i = 0; i < val.length; i++){
var a = val.substr(i,1);
if(encodeURIComponent(a).length > 3){
console.log(a, encodeURIComponent(a));
var re = new RegExp(encodeURIComponent(a),"ig");
valEnc.replace(re," ");
};
};
console.log(decodeURIComponent(valEnc));
This code works and logs me the unwanted chars but it can not replace them with spaces, what am i doing wrong? Thanks.

You seem to be using regular expressions unnecessarily here. One way to do this is to build up the result string one character at a time:
val = "Angelina’s";
valEnc = "";
for(var i = 0; i < val.length; i++){
var a = val.substr(i,1);
var e = encodeURIComponent(a);
if(e.length <= 3){
valEnc += e;
}
}
console.log(decodeURIComponent(valEnc));

Related

How to remove everything from string after the last character?

I am practicing and I wrote length of last word in JS.
var lengthOfLastWord = function (s) {
var words = s.split(" ");
var len = words.length;
for (let i = 0; i < len; i++) {
if (len == 1){
var last_element = words[0];
}
else {
var last_element = words[len - 1];
}
return last_element.length;
}
}
But it doesn't work well if
s = 'a '
or
s = 'Hello.'
How to write substring to remove everything except characters?
I would suggest you to try the following code:
s = 'Hello. '
s.trim();
The trim() function removes all whitespaces from your String.
You can use regex to replace charachters those are not in a-z or A-Z or 0-9. or simply just use \W in regex expression :
var last_element = words[len - 1].replace(/\W/g, "");

What is wrong with the logic of my character changing function?

I've tried to create a character changing function for strings, it suppose to change all the "-" to "_", and it only does it for the first character and leaves the rest. If someone could explain it would be grate.
function kebabToSnake(str) {
var idNum = str.length;
for(var i = 0; i <= idNum; i++) {
var nStr = str.replace("-", "_");
}
return nStr;
}
var nStr = str.replace("-", "_");
So, on each iteration, you're replacing the first found - character in the original string, not the string that you've already replaced characters from already. You can either call .replace on just one variable that you reassign:
function kebabToSnake(str) {
var idNum = str.length;
for(var i = 0; i < idNum; i++) {
str = str.replace("-", "_");
}
return str;
}
console.log(kebabToSnake('ab-cd-ef'));
(note that you should iterate from 0 to str.length - 1, not from 0 to str.length)
Or, much, much more elegantly, use a global regular expression:
function kebabToSnake(str) {
return str.replace(/-/g, '_');
}
console.log(kebabToSnake('ab-cd-ef'));

Counting vowels in javascript

I use this code to search and count vowels in the string,
a = "run forest, run";
a = a.split(" ");
var syl = 0;
for (var i = 0; i < a.length - 1; i++) {
for (var i2 = 0; i2 < a[i].length - 1; i2++) {
if ('aouie'.search(a[i][i2]) > -1) {
syl++;
}
}
}
alert(syl + " vowels")
Obviously it should alert up 4 vowels, but it returns 3.
What's wrong and how you can simplify it?
Try this:
var syl = ("|"+a+"|").split(/[aeiou]/i).length-1;
The | ensures there are no edge cases, such as having a vowel at the start or end of the string.
Regarding your code, your if condition needs no i2
if('aouie'.search(a[i]) > -1){
I wonder, why all that use of arrays and nested loops, the below regex could do it better,
var str = "run forest, run";
var matches = str.match(/[aeiou]/gi);
var count = matches ? matches.length : 0;
alert(count + " vowel(s)");
Demo
Try:
a = "run forest, run";
var syl = 0;
for(var i=0; i<a.length; i++) {
if('aouie'.search(a[i]) > -1){
syl++;
}
}
alert(syl+" vowels")
First, the split is useless since you can already cycle through every character.
Second: you need to use i<a.length, this gets the last character in the string, too.
The simplest way is
s.match(/[aeiou]/gi).length
You can use the .match to compare a string to a regular expression. g is global which will run through the entire string. i makes the string readable as upper and lower case.
function getVowels(str) {
var m = str.match(/[aeiou]/gi);
return m === null ? 0 : m.length;
}

Split a string using regex

I have a string and I want is split into an array so that it is split by '+' unless it is inside brackets
E.g. the string
"abc+OR+def+OR+(abc+AND+def)"
becomes
["abc", "OR", "def", "OR", "(abc+AND+def)"]
and the string
"(abc+AND+cde)+OR+(abc+AND+(cde+AND+fgh))"
becomes
["(abc+AND+cde)", "OR", "(abc+AND+(cde+AND+fgh)"]
Is it possible to do this using regular expressions?
You can do this with regex, but only with that languages that support recursive regular expression (for example, perl or any language wit PCRE).
It is not easy with JavaScript regexes, because they do not support recursion.
But it is possible using XRegExp using additional plugin:
http://xregexp.com/plugins/#matchRecursive
Also please check these two links:
http://blog.stevenlevithan.com/archives/regex-recursion
http://blog.stevenlevithan.com/archives/javascript-match-nested
I don't think you could do this with regex. EDIT: per Silver, you could use regex.
One way would be to just parse the string character by character. I'll edit my answer with code in a minute.
EDIT: Here's a sample implementation (note: untested, may have a bug or two):
function parseString (str) {
var splitStr = [], parentheses = 0, i = 0
for (var j = 0; j < str.length; j++) {
if (str[j] == '+' && !parentheses)
i++
else if (str[j] == '(')
parentheses++
else if (str[j] == ')')
parentheses--
else
splitStr[i] += str[j]
}
return splitStr
}
You can use the match method of String object to do this and use the following regex:
stringObj.match(/([a-zA-Z]+)|([(]([a-zA-Z]+[+])+[a-zA-Z]+[)])+/gi);
This regular expression would suit your needs.
(?!=\([\w\+]+)\+(?![\w+\+]+\))
See it in action here.
There is one small problem: Negative lookbehind (?!=...) is not implemented in the javascript regular expression parser.
For anyone who is learning regular expressions, here is a walkthrough:
(?!=\([\w\+]+) is a negative lookbehind. It means "not preceeded by ..." In this case, we're looking for something not preceeded by (lettersOr+.
\+ is what we are looking for. A plus sign (escaped)
(?![\w+\+]+\)) is a negative lookahead. It means "not followed by ..." In this case, we're looking for something not followed by lettersOr+)
This function should work for you:
var PARENTH_STRING_PLACE_HOLDER = '__PARSTRINGHOLDER__';
var splitPlusNoParenthesis = function(str){
//Replace the parenthStrings with the placeholder
var parenthStrings = getParenthesizedStrings(str);
for(var i = 0; i < parenthStrings.length; i++){
str = str.replace(parenthStrings[i], PARENTH_STRING_PLACE_HOLDER);
}
//Split on '+'
var splitString = str.split('+');
//Replace all placeholders with the actual values
var parIndex = 0;
for(var i = 0; i < splitString.length; i++){
if(splitString[i] === PARENTH_STRING_PLACE_HOLDER){
splitString[i] = parenthStrings[parIndex++];
}
}
return splitString;
};
var getParenthesizedStrings = function(str){
var parenthStrings = [];
for(var startIndex = 0; startIndex < str.length; startIndex++){
if(str[startIndex] === '('){
var parenthCount = 1;
var endIndex = startIndex + 1;
for(; endIndex < str.length; endIndex++){
var character = str[endIndex];
if(character === '('){
parenthCount++;
} else if(character === ')'){
parenthCount--;
}
if(!parenthCount){
parenthStrings.push(str.substring(startIndex, endIndex + 1));
break;
}
}
startIndex = endIndex;
}
}
return parenthStrings;
};
Here's a fiddle to test.

How do i get numbers from this string?

i have this string:
var s = 'http://xxxxxxx.xxx/abcd123456789?abc=1';
how do i get digits 123456789 (between "d" and "?") ?
these digits may vary. the number of digits may vary as well.
How do i get them?? Regex? Which one?
try
'http://xxxxxxx.xxx/abcd123456789?abc=1'.match(/\d+(?=\?)/)[0];
// ^1 or more digits followed by '?'
Try
var regexp = /\/abcd(\d+)\?/;
var match = regexp.exec(input);
var number = +match[1];
Are the numbers always between "abcd" and "?"?
If so, then you can use substring():
s.substring(s.indexOf('abcd'), s.indexOf('?'))
If not, then you can just loop through character by character and check if it's numeric:
var num = '';
for (var i = 0; i < s.length; i++) {
var char = s.charAt(i);
if (!isNaN(char)) {
num += char;
}
}
Yes, regex is the right answer. You'll have something like this:
var s = 'http://xxxxxxx.xxx/abcd123456789?abc=1';
var re = new RegExp('http\:\/\/[^\/]+\/[^\d]*(\d+)\?');
re.exec(s);
var digits = $1;

Categories

Resources