Hello I would like to make a javascript function to return 7 words before and after a math to an specific keyword
I tried as follows:
function myFunction(text) {
b=text.match("(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,7}"+text+"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,7}");
return b;
However when I search in my text "create" I justo got:
create
My desired output would be:
the Community, and view patterns you create or favorite in My Patterns. Explore results
My complete code looks as follows, with my corresponding string called Text, so I would like to appreciate the support to overcome this task.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var Text='RegExr was created by gskinner.com, and is proudly hosted by Media Temple. Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported. The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns. Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.'
function myFunction(text) {
b=text.match("(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,7}"+text+"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,7}");
return b;
}
document.getElementById("demo").innerHTML = myFunction("create");
</script>
</body>
</html>
Regular expressions aren't a great tool for this type of task. I would recommend using split to break your sentence into an array of words and then indexOf to find the matching word and print the adjacent words.
Here's a working example:
let sentence = "blah blah blah the Community, and view patterns you create or favorite in My Patterns. Explore results blah blah blah";
let words = sentence.split(" ");
let index = words.indexOf("create");
let result = [];
if (index > -1) {
for (i=index-7; i < (index+8); i++) {
result.push(words[i]);
}
}
console.log(result.join(" "));
That's the gist of it, but you'll need to modify my code sample to take into account edge cases (i.e., multiple matching words, less than 7 words preceding/following the matching word).
You can split the text into a words array and find the index of the word then use Array#slice() and Array#join()
Following also removes . and , punctuation for matching in case that word includes such punctuation before the following space and normalizes case match
var Text = 'RegExr was created by gskinner.com, and is proudly hosted by Media Temple. Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported. The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns. Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.'
var term = 'create',
words = Text.split(' '),
index = words.findIndex(s => s.replace(/,|\.$/, '').toLowerCase() === term.toLowerCase()),
start = index > 6 ? index - 7 : 0;
var res = words.slice(start, index + 8).join(' ')
console.log(res)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With