Object Oriented Programming Articles - Page 259 of 701

Lookbehind Assertions JavaScript Regular Expressions

AmitDiwan
Updated on 15-Jul-2020 12:57:57

186 Views

Lookbehind allows matching a pattern only if there’re something before.Following is an example −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample, .result {       font-size: 20px;       font-weight: 500;    } Look behind Assertions Regular Expressions The price for 4 tables are $50 , $99 , $121 and $150. CLICK HERE Click on the above button to extract the table price using lookbehind    let sampleEle = document.querySelector(".sample").innerHTML;    let btnEle = document.querySelector(".btn");    let resEle = document.querySelector(".result");    const priceRegex=/(? {       resEle.innerHTML = "Table price are = "+sampleEle.match(priceRegex);    }); OutputThe above code will produce the following output −On clicking the ‘CLICK HERE’ button −

Unicode Property Escapes JavaScript Regular Expressions

AmitDiwan
Updated on 15-Jul-2020 12:56:25

238 Views

The Unicode property escapes regular expressions, allow us to match characters based on their Unicode properties by using the flag u.ExampleFollowing is an example − Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample, .result {       font-size: 20px;       font-weight: 500; } Unicode Property Escapes JavaScript Regular Expressions Hello 😆😀 World 🙂😊 CLICK HERE Click on the above button to extract the emojis using regex    let sampleEle = document.querySelector(".sample").innerHTML;    let btnEle ... Read More

How can I disable JavaScript click function if link starts with #?

AmitDiwan
Updated on 15-Jul-2020 12:57:55

447 Views

For this, use preventDefault() in JavaScript. Following is the JavaScript code −Example Live Demo Document Press Me to see logs Press Me to see logs in console Nothing will happen    $(function(){       $("a:not([href='#'])").click(function(event){          event.preventDefault();          console.log("You have pressed me!!!!!");       });    }); To run the above program, save the file name anyName.html(index.html) and right click on the file. Select the option Open with live server in VS code editor.OutputIf you click the two links, Press Me ... Read More

How to disable default behavior of when you press enter with JavaScript?

AmitDiwan
Updated on 14-Jul-2020 17:18:15

370 Views

For this, you need to use keyDown as well as preventDefault(). Following is the JavaScript code −Example Live Demo Document    const pressEnter = (event) => {       if (event.key === "Enter") {          event.preventDefault();       }    };    document.getElementById("disableDefaultTime").addEventListener("keydown",    pressEnter); To run the above program , just save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VSCode editor.OutputFollowing is the output. When you press Enter key nothing will be displayed.You need to use keyDown to get time as in the below screenshot −

Mobile

How to disable future dates in JavaScript Datepicker?

AmitDiwan
Updated on 14-Jul-2020 17:20:31

14K+ Views

In order to disable future dates, you need to use maxDate and set the current date. Following is the JavaScript code −Example Live Demo Document The selected date is as follows:    $(document).ready(function () {       var currentDate = new Date();       $('.disableFuturedate').datepicker({       format: 'dd/mm/yyyy',       autoclose:true,       endDate: "currentDate",       maxDate: currentDate       }).on('changeDate', function (ev) {          $(this).datepicker('hide');       });       $('.disableFuturedate').keyup(function () {     ... Read More

Java program to find Maximum and minimum element’s position in a list

AmitDiwan
Updated on 07-Jul-2020 09:54:25

441 Views

To find the maximum and minimum element’s position in a list, the Java program is as follows −Example Live Demoimport java.util.*; import java.util.Arrays; import java.util.Collections; public class Demo{    public static int index_val(int my_arr[], int t){       if (my_arr == null){          return -1;       }       int len = my_arr.length;       int i = 0;       while (i < len){          if (my_arr[i] == t){             return i;          } else {         ... Read More

Program to Iterate over a Stream with Indices in Java 8

AmitDiwan
Updated on 07-Jul-2020 09:52:03

666 Views

To iterate over a Stream with Indices in Java 8, the code is as follows −Example Live Demoimport java.util.stream.IntStream; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class Demo{    public static void main(String[] args){       String[] my_array = { "T", "h", "i", "s", "s", "a", "m", "p", "l", "e" };       AtomicInteger my_index = new AtomicInteger();       System.out.println("The elements in the string array are :");       Arrays.stream(my_array).map(str -> my_index.getAndIncrement() + " -> " + str).forEach(System.out::println);    } }OutputThe elements in the string array are : 0 -> T 1 -> h 2 -> i 3 ... Read More

Java program to print unique values from a list

AmitDiwan
Updated on 08-Nov-2024 22:30:18

2K+ Views

In this article, we will learn to print unique values from a List in Java. This program will use a loop-based approach to identify and display only the values that appear once, skipping any duplicates. This approach is helpful when working with datasets where you want to remove repeated items and focus on distinct entries. Problem Statement Write a program in Java to print unique values from a list. Below is a demostration of the same − Input 55, 67, 99, 11, 54, 55, 88, 99, 1, 13, 45 Output The distinct elements in the array are 55 67 99 11 ... Read More

Java program to find all close matches of input string from a list

Alshifa Hasnain
Updated on 17-Dec-2024 03:34:20

794 Views

Finding close matches to a string from a list of words is a common problem in string manipulation and pattern recognition. This article demonstrates two effective approaches for solving this problem in Java. The first approach utilizes string encoding to identify similar patterns, while the second approach leverages the Levenshtein Distance algorithm to find approximate matches. String Encoding Approach The string encoding approach is a clever technique where each string is transformed into a unique encoded format. Strings with the same encoded pattern are considered matches HashMap A HashMap in Java is a collection that stores key-value pairs, where keys ... Read More

Java program to remove all duplicates words from a given sentence

AmitDiwan
Updated on 23-Oct-2024 17:29:44

1K+ Views

In this article, we will learn to remove duplicate words from a given sentence in Java. The first method uses Java Streams to remove duplicates and join the words back into a sentence using the distinct() and Collectors.joining() methods. The second method uses a Set to automatically filter out duplicate words and a StringJoiner to combine the words back into a sentence. Both methods will give a sentence with only unique words. Different approaches Following are the different approaches to remove all duplicate words from a given sentence − Using Java Streams ... Read More

Advertisements