I have tried to write the regex to remove all white spacing, special charcters, numbers from a string and just leave the letters.
For example, if I had the string
3388#accffiillnnoooorrsttttttuy I would want the following to be returned:
accffiillnnoooorrsttttttuy
I thought this would work but for some reason it doesn't appear to be doing what I expect
var letterPattern = /[a-zA-Z]+/g;
var string = string.match(letterPattern)
You probably want this:
var letterPattern = /[a-zA-Z]+/g;
var matches = '3388#accffiillnnoooorrsttttttuy'.match(letterPattern);
var string = matches[0];
Your regex is correct, the usage of match is incomplete though.
var letterPattern = /[a-zA-Z]+/g;
var matches = string.match(letterPattern);
if (matches) {
string = matches[0];
}
Use the following Regex
var string = "3388#accffiillnnoooorrsttttttuy";
string.replace(/[^a-zA-Z]/gi,'');
Check it:
<button onclick="myFunction()">Parse the String</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "3388#accffiillnnoooorrsttttttuy";
var patt1 = /[a-zA-Z]/g;
var result = str.match(patt1);
var resultString = result.join("");
document.getElementById("demo").innerHTML = resultString;
}
Related
i have problems inserting character in result of regex. I need do some like this:
var str = "Hello world, hello";
var regX = /he/ig;
The result have to be a string like this:
console.log(result);
<mark>He</mark>llo world, <mark>he</mark>llo"
I tried using this code:
r = /he/ig;
str = "Hello world Hello";
var match, indexes = [];
while (match= r.exec(str)){
indexes.push([match.index, match.index+match[0].length]);
}
indexes.forEach( (element) => {
var strStart = str.substring(0,element[0]);
var strBetween = "<mark>"+str.substring(element[0],element[1])+"</mark>";
var strEnd = str.substring(element[1],str.length);
str = strStart.concat(strBetween,strEnd);
});
console.log(str); //<mark>He</mark>llo worl<mark>d </mark>Hello
I understand where is the error, but i don't kwon how solve that.
You can do this with the .replace() method:
var str = "Hello world hello";
var result = str.replace(/he/ig, "<mark>$&</mark>");
The $& in the replacement string means that the matched text should be substituted.
var str = '#/promotionalMailer/test1';
output should be ==> #/promotionalMailer
I want the string before the second slash '/'
I have tried this so far:
var str = '#/promotionalMailer/test1';
var match = str.match(/([^\/]*\/){2}/)[0];
alert(match);
But it comes with the second slash.
try split, slice and join
var str = '#/promotionalMailer/test1';
console.log( str.split("/").slice(0,2).join("/"));
For example,
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.split('/').slice(0, 2).join('/')
document.write('<pre>'+JSON.stringify(result,0,3));
If you want regexes, then
var str = '#/promotionalMailer/test1/foo/bar/baz';
result = str.match(/[^\/]*\/[^\/]*/)[0]
document.write('<pre>'+JSON.stringify(result,0,3));
I am trying to split a string's characters into variables like this:
<script>
var string = "hello";
//After splitting:
var stringAt1 = "h";
var stringAt2 = "e";
var stringAt3 = "l";
var stringAt4 = "l";
var stringAt5 = "o";
</script>
Could somebody give an example of how this can be done?
String.prototype.split() function can be used for requirement.
The split() method splits a String object into an array of strings by separating the string into substrings.
Usage
var string = "hello";
var arr = string.split('');
var stringAt1 = arr[0];
alert(stringAt1)
Split according to the non-word boundary \B (which matches between two word characters or two non-word characters).
var string = "hello";
alert(string.split(/\B/))
Then assign the splitted parts to separate variables.
In ES5 there's no other way than
var splitted = "abc".split("");
var char0 = splitted[0];
var char1 = splitted[1];
etc
In ES6 it's much easier:
var [char0, char1, char2] = "abc";
I have string
var str = "Ahora MXN$1,709.05" and wanted to get only
"MXN$1,709.05" from this.
Can someone please help me?
You can use substring or replace. With replace you are going to replace something with nothing.
replace
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.replace(sub,'');
substring
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.substring(sub.length);
JsFiddle
You can use either substring or Regex
Using substring
var str = "Ahora MXN$1,709.05";
var result = str.substring('Ahora '.length);
console.log(result);
Using Regex
var str = "Ahora MXN$1,709.05";
var myRegexp = /Ahora\s(.*?)(?:\s|$)/g;
var match = myRegexp.exec(str);
console.log(match[1]);
I have some strings like:
str1 = "Point[A,B]"
str2 = "Segment[A,B]"
str3 = "Circle[C,D]"
str4 = "Point[Q,L]"
Now I want to have function that gives me character after "[" and the character before "]". How could I make something like that ?
try this one...
var str = "Point[A,B]";
var start_pos = str.indexOf('[') + 1;
var end_pos = str.indexOf(']',start_pos);
var text_to_get = str.substring(start_pos,end_pos)
alert(text_to_get);
You'd need regex to do that
var matches = /\[(.*?)\]/.exec(str1);
alert(matches[1]);
You can use match() to extract the characters:
str.match(/\[(.*)\]/)[1]
A safer way would be:
var matches = str.match(/\[(.*)\]/);
if(matches) {
var chars = matches[1];
}
Here's an approach which avoids regex.
var str = "Point[A,B]";
var afterOpenBracket = str.split("[")[1]; // returns "A,B]"
var bracketContents = afterOpenBracket.split("]")[0]; // returns "A,B"
There, pretty simple! bracketContents now contains the entirety of the text between the first set of brackets.
We can stop here, but I'll go a step further anyway and split up the parameters.
var parameters = bracketContents.split(","); // returns ["A", "B"]
Or in case u have more [A,C,D,B] and don't want to use regex:
var str1 = "Point[A,C,D,B]";
function extract(str1){
var a = str1.charAt(str1.indexOf('[')+1);
var b = str1.charAt(str1.indexOf(']')-1);
return [a, b];
//or
//a.concat(b); //to get a string with that values
}
console.log(extract(str1));