Find pattern in every line with JavaScript - javascript

Lets say I have the following string
def name1
toSomething
def name2
toSomethingElse
How could I find name1 and name2.
Using string.match(/(?:def) (.*)/) gives me:
name1
toSomething
def name2
toSomethingElse
So how can I search the string line by line?

You could try with:
/(?:def)\s+([^\s]+)/g
In the first group you will have what you need (name1, etc).
See here: http://regex101.com/r/rH0yH8
An example of how to run it:
var s = "def name1\ntoSomething\n\ndef name2 \ntoSomethingElse"
var re = /(?:def)\s+([^\s]+)/g
while (x = re.exec(s)) {
alert(x[1])
}

/^def (.*)/gm
will do it. The g flag matches every instance, and the m flag causes the ^ to match at the beginning of a line instead of at the beginning of the input only.
With .exec, you can loop and get the results one at a time.
var myString = 'def foo\n boo\n\ndef bar\n far\n';
for (var re = /^def (.*)/gm, match; (match = re.exec(myString));) {
alert(match[1]);
}
or you can use .match on the string to get all of the results in one array, but that doesn't give you the capturing groups.
var myString = 'def foo\n boo\n\ndef bar\n far\n';
var re = /^def (.*)/gm;
var matches = myString.match(re);

Your regex should just give you the match on the first line, since . does not match newlines.
That said, try /(?:def)\s+(\S*)/

Related

Display characters other than alphabets using reqular expression

I have tried to display characters other than alphabets in the particular string but it is displaying only the first char.
var myArray = /[^a-zA-Z]+/g.exec("cdAbb#2547dbsbz78678");
The reason it is only displaying the first character is because with using exec and the g modifier (global), this method is meant to be used in a loop for getting all sub matches.
var str = "cdAbb#2547dbsbz78678";
var re = /[^a-zA-Z]+/g;
var myArray;
while (myArray = re.exec(str)) {
console.log(myArray[0]);
}
Output
#2547
78678
If you were wanting to combine the matches you could use the following.
var str = "cdAbb#2547dbsbz78678",
res = str.match(/[\W\d]+/g).join('');
# => "#254778678"
Or do a replacement
str = str.replace(/[a-z]+/gi, '');
You can do:
"cdAbb#2547dbsbz78678".match(/[^a-zA-Z]+/g).join('');
//=> #254778678
RegExp.exec with g (global) modifier needs to run in loop to give you all the matches.

Replace last occurrence word in javascript [duplicate]

This question already has answers here:
JavaScript: replace last occurrence of text in a string
(16 answers)
Closed 8 years ago.
I have a problem with replacing last word in JS, I am still searching solution but i cannot get it.
I have this code:
var string = $(element).html(); // "abc def abc xyz"
var word = "abc";
var newWord = "test";
var newV = string.replace(new RegExp(word,'m'), newWord);
I want replace last word "abc" in this string, but now I can only replace all or first occurrence in string. How can I do this? Maybe is not good way?
Here is an idea ....
This is a case-sensitive string search version
var str = 'abc def abc xyz';
var word = 'abc';
var newWord = 'test';
// find the index of last time word was used
// please note lastIndexOf() is case sensitive
var n = str.lastIndexOf(word);
// slice the string in 2, one from the start to the lastIndexOf
// and then replace the word in the rest
str = str.slice(0, n) + str.slice(n).replace(word, newWord);
// result abc def test xyz
If you want a case-insensitive version, then the code has to be altered. Let me know and I can alter it for you. (PS. I am doing it so I will post it shortly)
Update: Here is a case-insensitive string search version
var str = 'abc def AbC xyz';
var word = 'abc';
var newWord = 'test';
// find the index of last time word was used
var n = str.toLowerCase().lastIndexOf(word.toLowerCase());
// slice the string in 2, one from the start to the lastIndexOf
// and then replace the word in the rest
var pat = new RegExp(word, 'i')
str = str.slice(0, n) + str.slice(n).replace(pat, newWord);
// result abc def test xyz
N.B. Above codes looks for a string. not whole word (ie with word boundaries in RegEx). If the string has to be a whole word, then it has to be reworked.
Update 2: Here is a case-insensitive whole word match version with RegEx
var str = 'abc def AbC abcde xyz';
var word = 'abc';
var newWord = 'test';
var pat = new RegExp('(\\b' + word + '\\b)(?!.*\\b\\1\\b)', 'i');
str = str.replace(pat, newWord);
// result abc def test abcde xyz
Good luck
:)
// create array
var words = $(element).html().split(" ");
// find last word and replace it
words[words.lastIndexOf("abc")] = newWord
// put it back together
words = words.join(" ");
You can use lookahead to get last word in a sentence:
var string = "abc def abc xyz";
var repl = string.replace(/\babc\b(?!.*?\babc\b)/, "test");
//=> "abc def test xyz"
You want to both:
match abc
check that there isn't another abc afterward in the string
So you can use:
abc(?!.*abc)
(?!...) is a negative lookahead, it will fail the whole regex match if what's inside the lookahead is matched.
Also be careful as this would match abc in abcdaire: if you only want abc as a separate word you need to add word boundaries \b:
\babc\b(?!.*\babc\b)
I'm not too familiar with JavaScript but you can probably twist this to fit your needs:
(\b\w+\b)(.*)(\1) replace with \1\2+'your_key_word'
See the demo to see what I mean.
try
var string = $(element).html(); // "abc def abc xyz"
var word = "abc";
var newWord = "test";
var newV = string.replace(new RegExp(word+'$'), newWord);
You can use the fact that the replace method replaces only the first occurrence of the target string if used without the global flag, and try something like:
"abc def abc xyz abc jkl".split(' ').reverse().join(' ').replace('abc', 'test').split(' ').reverse().join(' ')

Javascript regular expression to capture characters

I have written the following regexp in Javascript:
var rule = /^<[a-zA-Z0-9].*>/;
And the I checked it against this string:
var str = "<string stringValue><string2 stringValue>";
And the I executed:
var res = rule.exec(str);
And the res retruns:
<string stringValue> <string2 stringValue2>
Everything works in the way I need. But I must change two things:
1- First capture each occurrence (i mean each block of tag, in my example there are two)
2- I should strip off the tags [<>] in returned value. Is it then possible?
Regular expressions are, by default, "greedy". The .* in your rule will match as many characters as possible while still succeeding. Also, since you used ^ at the start, your pattern would only match the tag at the start of your input. This is why you are matching too much currently.
/<([^>]*)>/
Will match either tag, and put the contents (without the < >) in a capture group.
To find every tag, you can run the regular expression multiple times. Adding /g to the end makes the expression global, which allows for this behaviour.
var rule = /<([^>]*)>/g,
match,
tags = [],
input = "<string stringValue><string2 stringValue>";
while (match = rule.exec(input)) {
tags.push(match[1]);
}
console.log(tags);
In each loop, match[1] refers to the first capture group, the parentheses in the expression.
It will return:
[ "string stringValue", "string2 stringValue" ]
I guess you want to get the key value pair.
Try the code below:
var rule = /<([a-zA-Z0-9]*)\s+([a-zA-Z0-9]*)>/g;
var str = "<string stringValue><string2 stringValue>";
var res;
while((res = rule.exec(str)) !== null) {
console.log("key: "+res[1]+" value: "+res[2]);
}
//output
//key: string value: stringValue
//key: string2 value: stringValue
Try like this:
var reg = /<[a-zA-Z0-9][^>]*>/g;
var str = "<string stringValue><string2 stringValue>";
var res = str.match(reg);
res = res ? res.map(function(i){return i.replace(/^<|>$/g, '')}) : res;
// res[0]: string value: stringValu
// res[1]: string2 value: stringValue
Good luck!

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

How can I remove all characters up to and including the 3rd slash in a string?

I'm having trouble with removing all characters up to and including the 3 third slash in JavaScript. This is my string:
http://blablab/test
The result should be:
test
Does anybody know the correct solution?
To get the last item in a path, you can split the string on / and then pop():
var url = "http://blablab/test";
alert(url.split("/").pop());
//-> "test"
To specify an individual part of a path, split on / and use bracket notation to access the item:
var url = "http://blablab/test/page.php";
alert(url.split("/")[3]);
//-> "test"
Or, if you want everything after the third slash, split(), slice() and join():
var url = "http://blablab/test/page.php";
alert(url.split("/").slice(3).join("/"));
//-> "test/page.php"
var string = 'http://blablab/test'
string = string.replace(/[\s\S]*\//,'').replace(/[\s\S]*\//,'').replace(/[\s\S]*\//,'')
alert(string)
This is a regular expression. I will explain below
The regex is /[\s\S]*\//
/ is the start of the regex
Where [\s\S] means whitespace or non whitespace (anything), not to be confused with . which does not match line breaks (. is the same as [^\r\n]).
* means that we match anywhere from zero to unlimited number of [\s\S]
\/ Means match a slash character
The last / is the end of the regex
var str = "http://blablab/test";
var index = 0;
for(var i = 0; i < 3; i++){
index = str.indexOf("/",index)+1;
}
str = str.substr(index);
To make it a one liner you could make the following:
str = str.substr(str.indexOf("/",str.indexOf("/",str.indexOf("/")+1)+1)+1);
You can use split to split the string in parts and use slice to return all parts after the third slice.
var str = "http://blablab/test",
arr = str.split("/");
arr = arr.slice(3);
console.log(arr.join("/")); // "test"
// A longer string:
var str = "http://blablab/test/test"; // "test/test";
You could use a regular expression like this one:
'http://blablab/test'.match(/^(?:[^/]*\/){3}(.*)$/);
// -> ['http://blablab/test', 'test]
A string’s match method gives you either an array (of the whole match, in this case the whole input, and of any capture groups (and we want the first capture group)), or null. So, for general use you need to pull out the 1th element of the array, or null if a match wasn’t found:
var input = 'http://blablab/test',
re = /^(?:[^/]*\/){3}(.*)$/,
match = input.match(re),
result = match && match[1]; // With this input, result contains "test"
let str = "http://blablab/test";
let data = new URL(str).pathname.split("/").pop();
console.log(data);

Categories

Resources