1

Let's say I have these strings (each new line being a separate string):

EducationLink
BioLink
InterestsLink

And I wanted to extract the part that's not "Link" using Javascript. How would I do this? The expected results are

Education
Bio
Interests

I tried a Regex, but I'm not very experienced with them, and it failed:

/^ (^Link) $/

Using string functions such as slice() and substr(), I only got the values to the right of the selected text, not to the left as desired.

Thanks for your help.

6 Answers 6

2

you can use .replace()

'EducationLink'.replace('Link', ''); //returns Education

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

The thing is you CAN use regex to do this replace, but being such a simple scenario (unless there is more to it) why would you overcomplicate things?

Sign up to request clarification or add additional context in comments.

3 Comments

Because if there's more than one line to work on. Because there might be Link in the middle of the string somewhere. Because there's just so much that could go wrong with plain string substitution. Apart from that, no reason.
Neither of these scenarios are true, so this seems like the best answer.
@Kolink no need to get your knickers in a twist. The asker plainly states he has several strings and goes on to list them.
2
text = input.replace(/Link(\n|$)/g,"\n");

3 Comments

If you assume that each line is an individual string, then it should go like this instead: text = input.replace(/Link$/, '');. I'm not sure which the OP meant, though.
Each line is a separate string, sorry about that.
The updated regex will still work, but you can simplify it to /Link$/g if you want.
1

You would need to put the Link into the end of your RegEx, such as:

^([A-Z]+)Link$

This matches:

  • One or more characters between A and Z
  • Then the word Link
  • Then the end of the string

The part in parentheses will be returned as a group.

Furthermore, if all you need to do is remove the suffix Link, you can use the replace() method in Javascript:

var x = myStr.replace('Link', '');

4 Comments

Oh, that was rather obvious. However, why did my RegEx not work?
Your RegEx doesn't make much sense. The ^ character is used to match the beginning of the string, or to negate characters in a bracket group. Such as [^Link] would mean any character that is neither L, nor i nor n nor k.
Ok, but I used normal parentheses (), not square brackets [].
I can't think of anything that would match your expression.
0

One way is to use String.split()MDN

var txt = 'EducationLink'.split('Link')[0]; // txt == 'Education'

Comments

0

Try This:

<script>

var str = "EducationLink";
index = str.search(/link/i);
str = str.substr(0,index);

</script> 

Comments

0

As others have pointed out, if all the strings end in 'Link', just use a replace. Otherwise, any of these methods will work fine:

var i = 0;
var strings = [
    'EducationLink',
    'BioLink',
    'InterestsLink'
];
var s = '';
var r = /^(.+)Link$/
for (i = 0; i < strings.length; i += 1) {
    s = strings[i];
    //s = s.substring(0, s.indexOf('Link')); //Uncomment for substring
    //s = s.match(r)[1]; //Uncomment for regex
    s = s.replace('Link', '');
    strings[i] = s;
}
console.log(strings);

Copy and paste into a browser console window to run.

To answer your question ("However, why did my RegEx not work?") in a comment on another answer, your RegEx does the following:

/^ (^Link) $/
//Each regex term on own line, commented below
^   //start of string
    //Literal space
(^Link) //Capturing group, string starting with 'Link'.
    //Literal space
$   //end of string

While you can have two "start of string" identifiers in a Regex, there will only ever be one "start of string". The ^ character is only a negation in a character class which is enclosed in square brackets, not curved. If you want to capture a literal caret (^), put a backslash in front of it (\^).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.