I want to extract title form the input using regular expression
var str = '<a href="http://example/caraft.html" title="Queen Size">';
var pat = "title="([^"]+?)";
var result = str.match(pat);
Getting output like : title="Queen Size"
Output need : Queen Size
Temporarily I have achieved by using sub string
result = result.substring(6, (result.length-1));
I cannot figure a way to do it with regular expression.
Thanks in adavance
If you need exactly regexp -- try this:
var str = '<a href="http://example/caraft.html" title="Queen Size">';
var pat = 'title="([^\"]+)?"';
var result = str.match(pat);
Try this out:- https://jsfiddle.net/ombtkp6p/
jQuery(function($) {
var str = '<a href="http://example/caraft.html" title="Queen Size">';
var result = $(str).attr("title");
})
JavaScript Solution:-
var str = '<a href="http://example/caraft.html" title="Queen Size">';
var temp = document.createElement('div');
temp.innerHTML = str;
var htmlObject = temp.firstChild;
var result = htmlObject.getAttribute("title")
var str = '<a href="http://example/caraft.html" title="Queen Size">',
pat = 'title="([^"]+?)"',
result = str.match(pat);
console.log(result[1]);
You have some problems with the quote character (") in your pattern. Furthermore str.match() returns an array containing the parentheses-captured matched results.
Alternatively you can use the Javascript regex syntax for your pattern:
var pat = /title="([^"]+?)"/;
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 have a String "SHELF-2-1-1-2-1", I need to remove "2" from that string and want the output to be "SHELF-1-1-2-1"
I tried:
var str = "SHELF-2-1-1-2-1";
var res = str.split("-");
How can I join the array to get "SHELF-1-1-2-1"?
This would work:
var str = "SHELF-2-1-1".split('-2').join('');
Sounds like you want to do a replace... Try:
var res = str.replace('-2', '');
var str = "SHELF-2-1-1";
var res = str.split("-");
res.pop(res.indexOf('2'));
var newStr = res.join('-');
This should also work for your updated question, as it will only remove the first 2 from the string
let str = "Hello India";
let split_str = str.split("");
console.log(split_str);
let join_arr = split_str.join("");
console.log(join_arr);
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]);
var str = "test's t\r and t\n";
str = str.replace(/'/g, "\'");
str = str.replace(/\r/g, "\\r");
str = str.replace(/\n/g,"\\n");
Is it possible to do these 3 replaces in single statement?
I want escape these particular chars. With out escaping it makes some problem. "\n" following chars goes to next line. While passing this as parameter it ll not get it as "\n" in the server.
Try this:
str.replace(/'|\r|\n/g, function($0) {
var trans = {"\r":"r", "\n":"n"};
return "\\" + (trans.hasOwnProperty($0) ? trans[$0] : $0);
})
You can also chain them:
var str = "test's t\r and t\n";
str = str.replace(/'/g, "\'").replace(/\r/g, "\\r").replace(/\n/g,"\\n");
var str = "test's t\r and t\n";
str = str.replace(/(\'|\r\n|\r|\n)/g, "\\");
alert("++++++++++++"+str+"++++++++++++");