jQuery JavaScript

JavaScript prefix and suffix of a string

How to find prefix and suffix of the word in javascript

First of all, you have to know – how to get the currently selected text on the page by using the JavaScript. After that, I’ll show you how to get javascript prefix and suffix detection method of a string.

So, Javascript provided a method for retrieving the current selection of the page by using window.getSelection(). After fetching the current selection, suppose that we want to show that some prefix and suffix world (characters) of the selected text. In such a case, I have explored the above algorithm for getting the wrapped text of the selected text.

How to get the currently selected text

Detail of currently selected text

Now, I am going to show you an example for getting start position, end position, prefix text, current text, suffix text and range of the text. By this function, you will definitely receive above all details.

Get prefix String in javaScript

Get suffix String in javaScript

Similarly, with the help of the above code, you’ll get the start and end position of the selected text.  By using this JavaScript code you’ll get these important value:


After that, the final code with the sample is here:

JavaScript prefix and suffix example

Published by Amit Pandey

Professional Android app Developer, Full Stack Web Developer, Blogger, Entrepreneur And Founder of Coding Issue. I am always trying to learn new things or new possible ways to do things.

Did you find this page helpful?


Leave a Reply

Your email address will not be published. Required fields are marked *