Categories
JavaScript Answers

How to Match a Whole Word in JavaScript?

Sometimes, we want to match a whole word in JavaScript.

In this article, we’ll look at how to match a whole word in JavaScript.

Match a Whole Word in JavaScript

To match a whole word in JavaScript, we can create a regex with the RegExp constructor.

Then we can call the test method on it to check if the string matches the words.

For instance, we can write:

const lookup = '\n\n\n\n\n\nPC Games\n\n\n\n';
const word  = lookup.trim() ;
const match = new RegExp(`\\b${word}\\b`).test('2 PC Games')
console.log(match)

We create the word string with the words we’re looking for.

Then we create the regex with the RegExp constructor.

'\\b' is the word boundary for the pattern. This will let us match the whole word string.

Then we call test on the regex with the string we want to check if it matches the pattern.

Therefore, match should log true since the string matches the pattern.

Conclusion

To match a whole word in JavaScript, we can create a regex with the RegExp constructor.

Then we can call the test method on it to check if the string matches the words.

Categories
JavaScript Answers

How to Loop Through a Date Range with JavaScript?

Sometimes, we want to loop through a date range with JavaScript.

In this article, we’ll look at how to loop through a date range with JavaScript.

Loop Through a Date Range with JavaScript

To loop through a date range with JavaScript, we can use a while loop.

For instance, we can write:

const start = new Date("02/05/2020");  
const end = new Date("02/10/2020");  
let loop = new Date(start);  
while (loop <= end) {  
  console.log(loop);  
  const newDate = loop.setDate(loop.getDate() + 1);  
  loop = new Date(newDate);  
}

We create the start and end date objects that are the start and end dates of the date range.

loop is the variable with the start date.

We increment loop in the while loop until it’s bigger than or equal to end .

To do that, we write:

const newDate = loop.setDate(loop.getDate() + 1);  
loop = new Date(newDate);

Therefore, we see the dates in the date range being looped through and logged to the console.

Conclusion

To loop through a date range with JavaScript, we can use a while loop.

Categories
JavaScript Answers

How to Extract Elements with an Even Index from an Array with JavaScript?

Sometimes, we want to extract elements with an even index from an array with JavaScript.

In this article, we’ll look at how to extract elements with an even index from an array with JavaScript.

Extract Even Elements of an Array with JavaScript

To extract elements with an even index from an array with JavaScript, we can use the array filter instance method.

For instance, we can write:

const arr = [4, 5, 7, 8, 14, 45, 76];
const filtered = arr.filter((element, index) => {
  return (index % 2 === 0);
});
console.log(arr)

to call filter with a callback that has index as the 2nd parameter.

We return index % 2 === 0 to filter out all the elements that have odd indexes.

Therefore, filtered is [4, 5, 7, 8, 14, 45, 76] .

Conclusion

To extract elements with an even index from an array., we can use the array filter instance method.

Categories
JavaScript Answers

How to Change the First Letter of Each Word to Upper Case with JavaScript?

Sometimes, we want to change the first letter of each word to upper case with JavaScript.

In this article, we’ll look at how to change the first letter of each word to upper case with JavaScript.

Change the First Letter of Each Word to Upper Case with JavaScript

To change the first letter of each word to upper case with JavaScript, we can use the JavaScript string’s replace method with a callback to change the first letter of each word to uppercase.

For instance, we can write:

const str = "hello world";
const newStr = str.toLowerCase().replace(/b[a-z]/g, (letter) => {
  return letter.toUpperCase();
});
console.log(newStr)

We call str.toLowerCase to convert str to lower case.

Then we call replace on it with a regex to match all lowercase letters that are the first letter of a word with /b[a-z]/g .

The callback gets the matched letter and call toUpperCase on it.

Therefore, newStr is 'Hello World' .

Conclusion

To change the first letter of each word to upper case with JavaScript, we can use the JavaScript string’s replace method with a callback to change the first letter of each word to uppercase.

Categories
JavaScript Answers

How to Add a Custom Button to the Toolbar that Calls a JavaScript Function with CKEditor?

Sometimes, we want to add a custom button to the toolbar that calls a JavaScript function with CKEditor.

In this article, we’ll look at how to add a custom button to the toolbar that calls a JavaScript function with CKEditor.

Add a Custom Button to the Toolbar that Calls a JavaScript Function with CKEditor

To add a custom button to the toolbar that calls a JavaScript function with CKEditor, we can add a command with the addCommand method.

Then we can add a method that calls the command with the addButton method.

For instance, we can add the editor with:

<script src="https://cdn.ckeditor.com/4.16.1/standard/ckeditor.js"></script>

<textarea id="container">How are you!</textarea>

Then we write:

const editor = CKEDITOR.replace('container');

editor.addCommand("mySimpleCommand", {
  exec(edt) {
    console.log(edt.getData());
  }
});

editor.ui.addButton('SuperButton', {
  label: "Click me",
  command: 'mySimpleCommand',
  toolbar: 'insert',
  icon: 'https://avatars1.githubusercontent.com/u/5500999?v=2&s=16'
});

to create the editor with:

const editor = CKEDITOR.replace('container');

Then we add a command with:

editor.addCommand("mySimpleCommand", {
  exec(edt) {
    console.log(edt.getData());
  }
});

The first argument is the name of the command.

The 2nd argument is an object with the exec method that has the code for the command.

We just call getData to get the input value of the text editor.

Next, we call ui.addButton with the button name the first argument.

Then 2nd argument has the settings for the button, which includes the command property set to the command that we want to run.

The icon is the URL for the icon.

Conclusion

To add a custom button to the toolbar that calls a JavaScript function with CKEditor, we can add a command with the addCommand method.

Then we can add a method that calls the command with the addButton method.