Get first letter of last word of each span - javascript

I'm trying to get the first letter of the last word in every span on the page. I will then add this as a class (used for filtering).
Here is what I have so far:
$('.each-artist span').each(function() {
var artistName = $(this).text().toLowerCase(); // get text, convert to lowercase
var firstLetterSplice = artistName.slice(0, 1); // get first letter
$(this).parent().parent('.each-artist').addClass(firstLetterSplice); // add as class
});
So, this works fairly successfully, it takes the text of each .each-artist span, lowercases it, splices it to find first letter then adds this first letter as a class.
The only issue is I need to get the last word of each .each-artist span, not the first, then get the first letter of this last word and apply it as a class.
I have a jsFiddle set up for testing:
http://jsfiddle.net/rZbwQ/

You're almost there, just split by word boundary and then get the last word
$('.each-artist span').each(function () {
var artistName = $(this).text().toLowerCase().split(/\b/);
var firstLetterSplice = artistName.pop().slice(0, 1);
$(this).closest('.each-artist').addClass(firstLetterSplice);
});
FIDDLE

Related

Split text - get middle of a word only: js-(daniela)Cta

How to get only the middle of this id attribute:
js-danielaCta
I only want to get the word daniela but this word can be dynamic.
I basically want to remove the "js-" and "Cta" everytime.
How to do this in javascript?
Use a regular expression, match everything in between in a group, and extract that matched group:
const extract = id => id.match(/^js-(.+)Cta$/)[1]
console.log(extract('js-danielaCta'));
console.log(extract('js-abcCta'));
console.log(extract('js-foobarCta'));
This would work unless you will have js- or Cta inside the middle word.
var sample = "js-danielaCta";
var middle = sample.replace("js-","").replace("Cta","");
console.log(middle);
You may be better off using match with regex.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
You can use substring, so if var og =js-danielaCta, then to get the middle, you do the indicies of the start and end letter, so var new = og.substring(3, 9)

Regext to match any substring longer than 2 characters

regex-pattern-to-match-any-substring-matching exact characters longer-than-two-characters-from-a-provided input,where ever exact string matches
Only pot or potato should be highlighted, instead of ota or ot, when user type pota and click search button.
Please find code below where matched string is highlighted.
// Core function
function buildRegexFor(find) {
var regexStr = find.substr(0,3);
for (var i = 1; i < find.length - 2; i++) {
regexStr = '(' + regexStr + find.substr(i+2,1) + '?|' + find.substr(i,3) + ')';
}
return regexStr;
}
// Handle button click event
document.querySelector('button').onclick = function () {
// (1) read input
var find = document.querySelector('input').value;
var str = document.querySelector('textarea').value;
// (2) build regular expression using above function
var regexStr = buildRegexFor(find);
// (3) apply regular expression to text and highlight all found instances
str = str.replace(new RegExp(regexStr, 'g'), "<strong class='boldtxt'>$1</strong>");
// (4) output
document.querySelector('span').textContent = regexStr;
document.querySelector('div').innerHTML = str;
};
consider "meter & parameter" as one string, if type meter in input box and click search button. meter should be highlighted as well as meter in parameter should highlight.Thanks in advance
Your for loop is set to go from i = 1, while i is less than find.length-2. find.length is 4. 4-2 is 2. So your for loop is set to go from i = 1 while i is less than 2. In other words, it's operating exactly once. I have no idea what you thought that for loop was going to do, but I'm betting that isn't it.
Prior to the for loop, regextr is set equal to the string pot (the first three characters of the find string. The first (and only) time through the for loop, it is set to a new value: the left paren, the existing value (pot), the fourth character of find (a), the question mark, the vertical bar, and three characters from find starting with the second. Put those together, and your regextr comes out to:
(pota?|ota)
That RegEx says to find either the string "pota" (with the a being optional, so "pot" also works) or the string "ota". So any instances of pota, pot, or ota will be found and highlighted.
If you just wanted "pota?", just eliminate the right half of that line inside the for loop. Better yet, replace the entire subroutine with just a line that appends the ? character to the find string.

Hide Div based on search content and text value in div

Hi firstly ty for looking at my code ^^
i have made a working example that hides the content based on the input but the problem is it looks letter for letter so lets say i have this text in my div "yes i know" i have yo start typing with the y to find it because if i type lets say "know" it wont find it
here is the example demo
and here is my code
I would like it to work that it would look for words in stead exact letter it starts
tyvm in advance ^^
$('#my-textbox').keyup(function() {
var value = $(this).val();
var exp = new RegExp('^' + value, 'i');
$('.panel-group .panel').each(function() {
var isMatch = exp.test($('.accordion-toggle', this).text());
$(this).toggle(isMatch);
});
});
simply remove the ^ from the RegExp. this means it has to start with said string
var exp = new RegExp(value, 'i');
On this link you can find more info about RegExp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Replace whole words only using Regex

I am trying to replace some letters in a textarea with some text from an input box.
The problem that I am facing is that it replaces all the values that are specified in CN.
For example, if CN is "A", then "A" will be replaced as well as "A" in "Apple". I want to replace only "A". I have tried using chordName = CN + " ", but that replaces values only where CN is in the middle of a sentence, not at the beginning of a line.
This is my code:
function doit(CN, SC) {
var rawLyrics = document.getElementById("ttbox").value;
var chordName = CN;
var spnCode = SC;
var res = rawLyrics.replace(new RegExp(chordName, 'g'), spnCode);
results.innerText = res;
}
I also have the problem where "results" textarea gets refreshed with the replaced words rather than changing only the required words i.e, I am using the above code to replace different words in a single block of text, so once I replace one word, and move on to the next, the previously replaced word comes back to it's previous state.
What am I doing wrong? Any solutions?

Find part of word before caret in textarea

I have a jQuery plugin that finds the caret position of a textarea. I implement it on the keyup function of the textarea, like this:
$("#editor").keyup(function () {
var textbox = $(this);
var end = textbox.getSelection().end;
});
I am wanting to find the word, or part of a word, before the caret. Words are delimited by any type of whitespace.
My main difficulty in doing this is dealing with line breaks. How can I find the word or part of a word immediately before the caret given the character index of the caret?
If you're using my jQuery textarea plug-in for this, the selection character positions are always relative to the value property of the textarea, regardless of how the browser handles line breaks. You could then get the last word as follows:
Note that you have to use the textarea's value property and not jQuery's val() method, which normalizes line breaks.
$("#editor").keyup(function () {
var textbox = $(this);
var end = textbox.getSelection().end;
var result = /\S+$/.exec(this.value.slice(0, end));
var lastWord = result ? result[0] : null;
alert(lastWord);
});

Categories

Resources