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.
Related
Here is my String:
var str1 = '#hello, world how are you. I #am good';
Now I want to split # prefixed worlds like #hello, #am etc to be stored in an array.
Desired output will be
var str2 = [#hello, #am];
can anyone guide me.
With a simple regex
\B#\w+/g
without a function :
var str1 = '#hello, world how are you. I #am good';
console.log(str1.match(/\B#\w+/g));
with a function :
getMatchedStrings("#hello, #world how are you. I #am good");
function getMatchedStrings(input){
var re = /\B#\w+/g;
var specials = [];
var match;
while(match = re.exec(input)){
specials.push(match[0]);
}
console.log(specials)
}
You may try more regex here :
https://regex101.com/r/rBuMrY/1
Output:
["#hello", "#am"]
Check this out, i add comment for easy understand this code
var str = '#hello, world how are you. I #am good';
str = str.split(' '); // split text to word array
str = str.filter(function(word){
return word.includes('#'); // check words that using #
}).map(function (word) {
return word.replace(/[^a-zA-Z^# ]/g, "") // remove special character except #
});
console.log(str) // show the data
var str1 = '#hello, world how are you. I #am good';
var reg = /(?:^|[ ])#([a-zA-Z]+)/;
var str = str1.split(" ");
//console.log(str.length);
for (var i = 0; i < str.length; i++) {
if (reg.test(str[i])) {
//your code to store match value in another array
console.log(str[i]);
}
}
Use Regex, I am not really good at it but just a try
var str1 = '#hello, world how are you. I #am good';
var str2 = str1.match(/#(.+?)[^a-zA-Z0-9]/g).map(function(match) { return match.slice(0, -1); });
console.log(str2);
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 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;
}
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));