Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
locate-character
Advanced tools
Readme
Get the line and column number of a particular character in a string.
npm install locate-character
, or get it from unpkg.com/locate-character.
To search for a particular character, using the index or a search string, use locate
:
import { locate } from 'locate-character';
const sample = `
A flea and a fly in a flue
Were imprisoned, so what could they do?
Said the fly, "let us flee!"
"Let us fly!" said the flea.
So they flew through a flaw in the flue.
`.trim();
// Using a character index
const index = sample.indexOf('fly');
locate(sample, index);
// -> { line: 0, column: 13, character: 13 }
// Using the string itself
locate(sample, 'fly');
// -> { line: 0, column: 13, character: 13 }
// Using the string with a start index
locate(sample, 'fly', { startIndex: 14 });
// -> { line: 2, column: 9, character: 76 }
If you will be searching the same string repeatedly, it's much faster if you use getLocator
:
import { getLocator } from 'locate-character';
const locate = getLocator(sample);
let location = locate(13);
// -> { line: 0, column: 13, character: 13 }
location = locate('fly', { startIndex: location.character + 1 });
// -> { line: 2, column: 9, character: 76 }
location = locate('fly', { startIndex: location.character + 1 });
// -> { line: 3, column: 8, character: 104 }
In some situations (for example, dealing with sourcemaps), you need one-based line numbers:
getLocator(sample, { offsetLine: 1 });
locate(sample, { offsetLine: 1 });
There's also an offsetColumn
option which is less useful in real-world situations.
MIT
FAQs
Get the line and column number of a specific character in a string
The npm package locate-character receives a total of 543,677 weekly downloads. As such, locate-character popularity was classified as popular.
We found that locate-character demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.