JavaScript RegExp ignore case flag vs global match flag - javascript

Here is a codesample for the flag ignore case. I was expected to receive only one match.
var str = "Sample text";
var a = new Array();
a = str.match(/S(am)(p)/i);
result
a = [Samp] [am] [p]
I was expected to have a = [Samp]
if you change i flag with g
var str = "Sample text";
var a = new Array();
a = str.match(/S(am)(p)/g);
surprise (at least for me) the result has only one element
a = [Samp]

The javascript regex API is extremely unintuitive as it does all sorts of magic depending on the g-flag.
I am just gonna cover how .match behaves:
Without g-flag .match returns an array of full match plus all the capture groups or null in case of no match.
With g-flag .match returns an array of all the full matches and capture groups don't make a difference. null if there are no matches.

a = str.match(/S(am)(p)/i);
return first matched string, $1 $2
a = str.match(/S(am)(p)/g);
return array of matched (only one here)
if your string is decalred like that
var str = "Sample text Sample text";
/g return Samp Samp, it is usefull when your regexp looks like /S(.m)(p)/g and string looks like "Sample text Simple text";

Related

JavaScript get first name and last name from string as array

I have a string that has the following format: <strong>FirstName LastName</strong>
How can I change this into an array with the first element firstName and second lastName?
I did this, but no luck, it won't produce the right result:
var data = [myString.split('<strong>')[1], myString.split('<strong>')[2]]
How can I produce ["firstName", "lastName"] for any string with that format?
In order to parse HTML, use the best HTML parser out there, the DOM itself!
// create a random element, it doesn't have to be 'strong' (e.g., it could be 'div')
var parser = document.createElement('strong');
// set the innerHTML to your string
parser.innerHTML = "<strong>FirstName LastName</strong>";
// get the text inside the element ("FirstName LastName")
var fullName = parser.textContent;
// split it into an array, separated by the space in between FirstName and LastName
var data = fullName.split(" ");
// voila!
console.log(data);
EDIT
As #RobG pointed out, you could also explicitly use a DOM parser rather than that of an element:
var parser = new DOMParser();
var doc = parser.parseFromString("<strong>FirstName LastName</strong>", "text/html");
console.log(doc.body.textContent.split(" "));
However, both methods work perfectly fine; it all comes down to preference.
Just match everything between <strong> and </strong>.
var matches = "<strong>FirstName LastName</strong>".match(/<strong>(.*)<\/strong>/);
console.log(matches[1].split(' '));
The preferred approach would be to use DOM methods; create an element and get the .textContent then match one or more word characters or split space character.
let str = '<strong>FirstName LastName</strong>';
let [,first, last] = str.split(/<[/\w\s-]+>|\s/g);
console.log(first, last);
/<[/\w\s-]+>|\s/g
Splits < followed by one or more word, space or dash characters characters followed by > character or space to match space between words in the string.
Comma operator , within destructuring assignment is used to omit that index from the result of .split() ["", "FirstName", "LastName", ""].
this is my approach of doing your problem. Hope it helps!
var str = "<strong>FirstName LastName</strong>";
var result = str.slice(0, -9).substr(8).split(" ");
Edit: it will only work for this specific example.
Another way to do this in case you had something other than an html
var string = "<strong>FirstName LastName</strong>";
string = string.slice(0, -9); // remove last 9 chars
string = string.substr(8); // remove first 8 chars
string = string.split(" "); // split into an array at space
console.log(string);

JS What's the fastest way to display one specific line of a list?

In my Javascript code, I get one very long line as a string.
This one line only has around 65'000 letters. Example:
config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...
What I have to do is replace all & with an break (\n) first and then pick only the line which starts with "path_of_code=". This line I have to write in a variable.
The part with replace & with an break (\n) I already get it, but the second task I didn't.
var obj = document.getElementById('div_content');
var contentJS= obj.value;
var splittedResult;
splittedResult = contentJS.replace(/&/g, '\n');
What is the fastest way to do it? Please note, the list is usually very long.
It sounds like you want to extract the text after &path_of_code= up until either the end of the string or the next &. That's easily done with a regular expression using a capture group, then using the value of that capture group:
var rex = /&path_of_code=([^&]+)/;
var match = rex.exec(theString);
if (match) {
var text = match[1];
}
Live Example:
var theString = "config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...";
var rex = /&path_of_code=([^&]+)/;
var match = rex.exec(theString);
if (match) {
var text = match[1];
console.log(text);
}
Use combination of String.indexOf() and String.substr()
var contentJS= "123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...";
var index = contentJS.indexOf("&path_of_code"),
substr = contentJS.substr(index+1),
res = substr.substr(0, substr.indexOf("&"));
console.log(res)
but the second task I didn't.
You can use filter() and startsWith()
splittedResult = splittedResult.filter(i => i.startsWith('path_of_code='));

How to match a string inside another string while ignoring whitespace

I have a string to search, and I need to match and return another string at the beginning of the search string. The string being searched may have whitespace in it, which needs to be ignored for the purpose of searching, but still returned accurately. The string to be matched will never have whitespace in it.
stringA = "ThisIsAString";
Should give the following results when compared to stringB:
stringB = "This Is A String"; //"This Is A String"
stringB = "ThisIsAlsoAString"; //undefined
stringB = "ThisIs A String With Extra Words At The End"; //"ThisIs A String"
stringB = "Hey, ThisIsAString"; //undefined
What's an efficient way to do this?
You can use \s* to match zero or more spaces. With the code below we put one of those matchers between each character.
const stringA = "ThisIsAString";
const tests = [
"This Is A String",
"ThisIsAlsoAString",
"ThisIs A String With Extra Words At The End",
"Hey, ThisIsAString",
];
const optionalSpaces = '\\s*';
const results = tests.map(test =>
test.match(
new RegExp(stringA.split('').join(optionalSpaces))
)
);
console.log(results);
An easy way to do this would be to remove all white space from the two things you are comparing.
var search = "Hello There";
var text = "HelloThere I'm Greg";
var found = text.replace(/ +?/g, '').indexOf(search.replace(/ +?/g, '')) !== -1;

Every string between given strings

I need an array of strings which are between two strings but when i use str.match the result is not what I expect:
var text = "first second1 third\nfirst second2 third\nfirst second3 third";
var middles = text.match(/first (.*?) third/g);
console.log(middles); //this should be ["second1", "second2", "second3"]
Result:
["first second1 third", "first second2 third", "first second3 third"]
Is there something I can try to get only the middle strings for each occurrence?
From the documentation for RegExp.prototype.exec():
If your regular expression uses the "g" flag, you can use the exec
method multiple times to find successive matches in the same string.
When you do so, the search starts at the substring of str specified by
the regular expression's lastIndex property (test() will also advance
the lastIndex property).
Applying this to your case:
var text = "first second1 third\nfirst second2 third\nfirst second3 third";
var middles = [], md, regex = /first (.*?) third/g;
while( md = regex.exec(text) ) { middles.push(md[1]); }
middles // ["second1", "second2", "second3"]

java script regex .match find only one result

i have this js code :
result = subject.match(/<a.*class="gallery_browser_thumbnail".*href="(.+)">/i);
i want to get href of multiple a tags on a html source
but it shows only 1 result
if i use /g at end of pattern it returns whole patterns but i just want only the href part
i mean -> (.+) this part
this is how i capture html input :
var subject = String(document
.getElementsByTagName("body")[0].innerHTML);
any help?
final working script :
var subject = String(document.getElementsByTagName("body")[0].innerHTML);
var regex = /<a.*class="gallery_browser_thumbnail".*href="(.+)">/gi;
var matched = null;
while (matched = regex.exec(subject)) {
alert(matched[1]);
}
Change to lazy match by adding the lazy quantifier ?:
result = subject.match(/<a.*?class="gallery_browser_thumbnail".*?href="(.+?)">/i);
You can use exec to test RegExp. Something like this:
var subject = String(document.getElementsByTagName("body")[0].innerHTML),
regexp = /<a.*class="gallery_browser_thumbnail".*href="(.+)">/gi, //g for global match
match = regexp.exec(subject),
result = [];
while(match != null){
result.push(match[1]); // the second value is matched group
match = regexp.exec(subject);
}

Categories

Resources