Marko Zekanović
Marko Zekanović

Reputation: 11

How to remove all characters (in my case numbers and dots) before any first letter of a string?

I have strings for example: "1. Hearing records (Tbr.1.t.4.)" or "23. Appointment details (Tbr.2.t.2.)".

I want to get "Hearing records (Tbr.1.t.4.)" and "Appointment details (Tbr.2.t.2.)".

How can I achieve this with javascript, preferably vanilla? I want to remove all characters (numbers and dots) that appear before the first letter, but after that leave everything unchanged.

Thank you

Upvotes: 0

Views: 84

Answers (6)

ch1ng1zz
ch1ng1zz

Reputation: 11

I tried to solve with some string methods.

const a=prompt();
arr=a.split(".");
const c=arr[0]+".";
const d=(a.replace(c,""));
console.log(d.trim());```

Upvotes: 1

ShiDel
ShiDel

Reputation: 11

I tried something simple in JS.Hope you liked :)

const first = "1. Hearing records (Tbr.1.t.4.)";
const second = "23. Appointment details (Tbr.2.t.2.)";

const numb = first.indexOf(".") + 1;
const numb2 = second.indexOf(".") + 1;


console.log(first.slice(numb)+ "\n" + second.slice(numb2));

Upvotes: 1

Maik Lowrey
Maik Lowrey

Reputation: 17594

You mean something like that?

let txt = "1. Hearing records (Tbr.1.t.4.)"
let newText = txt.replace(/\d+\.\s/g, "");
console.log(newText )

Upvotes: 2

Tushar Shahi
Tushar Shahi

Reputation: 20701

Step by step :

  1. Find first index of the character you are looking for.
  2. Get substring from string after the index (with some manipulation).

let s = "1. Hearing records (Tbr.1.t.4.)";
let indx = s.indexOf('.');
s = s.substring(indx+2);
console.log(s);

Upvotes: 2

Ali Mamedov
Ali Mamedov

Reputation: 139

I tried to solve this through JS. Here is my code:

let str = '1.Hearing records (Tbr.1.t.4.)';

        function deleteToLetter(str) {
            let indx = str.indexOf('.')
            str = str.slice(indx + 1);
            return str;
        }
        console.log(deleteToLetter(str));

Upvotes: 1

Muhammad Atif Akram
Muhammad Atif Akram

Reputation: 1315

const cases = ['1. Hearing records (Tbr.1.t.4.)','23. Appointment details (Tbr.2.t.2.)']

const filtered = cases.map((item)=>{
  return item.split('.').slice(1).join('').trim()
});
console.log(filtered)


const word = "1. Hearing records (Tbr.1.t.4.)";
console.log(word.split('.').slice(1).join('').trim())

I hope it will work. Cheers :)

Upvotes: 2

Related Questions