How to get particular string from one big string ? - javascript

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]);

Related

JavaScript regular expression to extracting title content

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="([^"]+?)"/;

Javascript regex get string before second /

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));

javascript to regex to just keep letters

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;
}

Split and join in JavaScript?

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);

JavaScript get character in sting after [ and before ]

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));

Categories