2

I'm trying to use Regex with my assignment.

2 problems here I can't solve:

problem 1: The 1st if the statement is meant to put the string into "pascal" case. The 2nd if statement into "camel" case. Regardless the statement it looks like executions stops after 1st statement. What do I do wrong here?

problem 2: My 1st if statement even though return result ("pascal") i can not figure out how to get rid of white spaces in my regex part of the code.

Please help. stuck 2nd day in a row.

const makeCase = function(input, type) {
  //place your code here
  
    let inType = type;
    let finalInput = (type === inType)
  
  
    if(finalInput) {
      return input.replace(/^(.)|\s+(.)/g, function(str){
        return str.toUpperCase();
      });
    } else if(finalInput) {
      return input.replace(/\W+(.)/g, function(str){
        return str.toUpperCase();
      });
    } 
  };

  console.log(makeCase("this is a string", "pascal"));  
  console.log(makeCase("this is a string", "camel"));


4 Answers 4

1

Perhaps do ucwords, then remove the spaces and then based on type, either upper or lower case first char.

function makeCase(str, type) {
  str = (str + '').replace(/^([a-z])|\s+([a-z])/g, $1 => $1.toUpperCase()).replace(/ /g, '')
  return str.charAt(0)[type === 'pascal' ? 'toUpperCase' : 'toLowerCase']() + str.slice(1)
}

console.log(makeCase("this is a string", "pascal")); // ThisIsAString
console.log(makeCase("this is a string", "camel"));  // thisIsAString

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

3 Comments

i obviously do not understand 100% of the code. I tried to extend it for other cases: console.log(makeCase("this is a string", "snake")); console.log(makeCase("this is a string", "kebab")); console.log(makeCase("this is a string", "title")); console.log(makeCase("this is a string", "vowel")); console.log(makeCase("this is a string", "consonant")); console.log(makeCase("this is a string", ["upper", "snake"]));. But I failed due to a lack of understanding. Can you help me with this? with explanations. Thanks in advance
they are all diff styles and don't fit in with "pascal or camel" whilst camel is simply pascal with lowercase first char which is handled by type === 'pascal' ? 'toUpperCase' : 'toLowerCase', my code above is only really suited for pascal or camel, to solve the original question. If you want to handle all the diff types, then you should use a bunch of if's and solve each type with a diff regex or manipulation, open a new question with what you have tried, simply passing in a diff string for type wont work.
Thanks a lot, Lawrence. Your explanation helped me to find separate solutions for each of the cases except the last one. I might post a question on the last one.
1

Rationale

The String.replace method only performs one replacement.

In the browser you can do String.replaceAll, however in node you can not.

I found that the simplest way to solve this is to split up the string where there is a space using split, and glue the words together without a space using join.

As one function

Here's a working example of how I would write it;

const makeCase = function(input, type) {
  //place your code here
  return input
    .split(' ')
    .map((word, index) => {
      if (index === 0 && type === 'camel') {
        return word
      } else {
        const [first, ...rest] = word;
        return `${first.toUpperCase()}${rest.join('')}`;
      }
    })
    .join('');
  }


console.log(makeCase("this is a string", "pascal"));
console.log(makeCase("this is a string", "camel"));

Creating replaceAll()

Creating your own replaceAll function could look like this.

export const replaceAll = (input, searchValue, replaceValue) => {
  return input.split(searchValue).join(replaceValue);
};

Note that string.split separator parameter can also make use of Regex.

Now you

Recommend trying to understand each part and writing your own variation.

Comments

0

This is wrong as finalInput will always be true:

    let inType = type;
    let finalInput = (type === inType)

Comments

0

One problem: put a flag i with g. It helps to ignore case.

1 Comment

It helps if you show an example of what you're sugesting.

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.